TreeView and databinding failed

岁酱吖の 提交于 2019-12-11 16:45:13

问题


I am working around the MVVM pattern and a TreeView.

I failed to bind the data models with the view. Here's my currently code:

MainWindow.xaml:

xmlns:reptile="clr-namespace:Terrario.Models.Reptile"
...
<TreeView x:Name="TrvFamily" Grid.Row="1" Grid.Column="0" ItemsSource="{Binding }">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate DataType="{x:Type reptile:Family}" ItemsSource="{Binding Items}">
                    <TextBlock Text="{Binding Name}" />
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        LoadFamily();
    }

MainWindow.xaml.cs:

    private void LoadFamily()
    {
        FamilyVM familiesVM = new FamilyVM();
        familiesVM.Load();
        TrvFamily.DataContext = familiesVM;
    }
}

ViewModels\FamilyVM:

class FamilyVM
{
    public ObservableCollection<Family> Families { get; set; }

    public void Load()
    {
        ObservableCollection<Family> families = new ObservableCollection<Family>();

        families.Add(new Family { ID = 1, Name = "Amphibian" });
        families.Add(new Family { ID = 2, Name = "Viperidae" });
        families.Add(new Family { ID = 3, Name = "Aranae" });

        Families = families;
    }
}

Models\Family.cs

    class Family
{
    public int ID { get; set; }
    public string Name { get; set; }
}

The TreeView still white, like without data.

Hope you have a issue ;)

Thanks per advance


回答1:


You're binding to the instance of FamilyVM rather than Families. That has no Name property, so you get nothing.

You should also always implement inotifypropertychanged on any viewmodel. You get a memory leak otherwise.

And you have no child collection on Family.

    <TreeView x:Name="TrvFamily" Grid.Row="1"
              ItemsSource="{Binding Families}">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate DataType="{x:Type local:Family}" ItemsSource="{Binding SomeCollectionYouDoNotHaveYet}">
                <TextBlock Text="{Binding Name}" />
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

It's usual to put the inotifypropertychanged stuff in a base class you inherit viewmodels from.

class FamilyVM : INotifyPropertyChanged
{
    private ObservableCollection<Family> families = new ObservableCollection<Family>();

    public ObservableCollection<Family> Families
    {
        get { return families; }
        set { families = value; NotifyPropertyChanged(); }
    }

    public void Load()
    {
        ObservableCollection<Family> families = new ObservableCollection<Family>();
        families.Add(new Family { ID = 1, Name = "Amphibian" });
        families.Add(new Family { ID = 2, Name = "Viperidae" });
        families.Add(new Family { ID = 3, Name = "Aranae" });
        Families = families;
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}


来源:https://stackoverflow.com/questions/53686025/treeview-and-databinding-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!