TreeView和数据绑定失败 [英] TreeView and databinding failed

查看:67
本文介绍了TreeView和数据绑定失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究MVVM模式和TreeView.

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:

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:

MainWindow.xaml.cs:

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

ViewModels \ FamilyVM:

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

Models\Family.cs

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

TreeView仍然是白色的,就像没有数据一样.

The TreeView still white, like without data.

希望您有问题;)

感谢前进

推荐答案

您将绑定到FamilyVM实例而不是Families. 没有Name属性,因此您一无所获.

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

您还应该始终在任何视图模型上实现已更改的inotify属性. 否则会导致内存泄漏.

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

您在Family上没有子代.

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>

通常将inotifypropertychanged的内容放在继承视图模型的基类中.

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));
    }
}

这篇关于TreeView和数据绑定失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆