C#C
C#2y ago
stigzler

WPF Treeview HierarchicalDataTemplate on Type with sub-collection

Hi Folks. I have the following two models (abridged for brevity) and mainViewModel:

internal class Gist
{
    public ObservableCollection<GistFile> GistFiles { get; set; } = new();

    public override string ToString()
    {
        return GistFiles.OrderBy(gf => gf.Filename).First().Filename;
    }
}

internal class GistFile
{
    public string Filename {  get; set; }

    public override string ToString()
    {
        return Filename;
    }
}

internal class MainWindowViewModel : ViewModelBase
{
    private ObservableCollection<Models.Gist> gists = new ObservableCollection<Models.Gist>();
    public ObservableCollection<Models.Gist> Gists { get => gists; set => SetProperty(ref gists, value); }    
}

and my xaml:
<TreeView x:Name="GistsTV"  ItemsSource="{Binding Gists}" Width="200">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Gists.Files}">
            <TextBlock Text="{Binding}"></TextBlock>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

I'm only getting the Gist displayed and not the GistFiles on a sub-level. I know I'm getting this wrapped around my head conceptually - what am I doing wrong?
Was this page helpful?