MVVM中的WPF-TreeView绑定

本文关键字:绑定 WPF-TreeView 中的 MVVM | 更新日期: 2023-09-27 18:09:26

我只是尝试实现简单的mvvm模型与WPF-TreeView与数据绑定。但是我发现了一些问题,希望你们中有人能帮我解决这个问题。

我有一个模型,在那里我定义了我自己的类:

public class ItemOfWorld : INotifyPropertyChanged
{
    #region Fields
    private ObservableCollection<ItemOfWorld> _Items;
    private string _Name;
    private ItemOfWorld _Parent;
    #endregion
    #region FieldDeclaration
    public ObservableCollection<ItemOfWorld> Items 
    { 
        get
        {
            return _Items;
        }
        set
        {
            _Items = value;
            OnPropertyChanged("Items");
        }
    }
    public string Name
    {
        get
        {
            return _Name;
        }
        set 
        {
            _Name = value;
            OnPropertyChanged("Name");
        }
    }
    public ItemOfWorld Parent
    {
        get
        {
            return _Parent;
        }
        set
        {
            _Parent = value;
            OnPropertyChanged("Parent");
        }
    }
    #endregion
    public ItemOfWorld(ItemOfWorld parent)
    {
        Items = new ObservableCollection<ItemOfWorld>();
        Parent = parent;
    }
    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion
}
public class MyWorld:ItemOfWorld
{
    public MyWorld():base(null)
    {
        Name = "MyWorld";
        Items.Add(new Ions(this));
    }
}
public class Ions : ItemOfWorld
{
    public Ions(ItemOfWorld parent)
        : base(parent)
    {
        Name = "Ions";
        Parent = parent;
    }
}
public class Molecules : ItemOfWorld
{
    public Molecules(ItemOfWorld parent)
        : base(parent)
    {
        Name = "Molecules";
        Parent = parent;
    }
}

我有一个ViewModel,在那里我创建了MyWorld类:

internal class ItemsOfWorldViewModel
{
    #region Fields
    private ItemOfWorld _MyItem;
    #endregion
    #region FieldDeclaration
    public ItemOfWorld MyItem
    {
        get
        {
            return _MyItem;
        }
        set
        {
            _MyItem = value;
        }
    }
    #endregion

    public ItemsOfWorldViewModel()
    {
        MyItem = new MyWorld();
    }
}

,最后,我有一个视图与TreeView工具箱,与简单的代码隐藏:

 ItemsOfWorldViewModel myworld=new ItemsOfWorldViewModel();
 TreeView1.DataContext = myworld;

我的问题是:我如何在我的树视图中显示我的层次结构而不使用本地源?

-MyWorld
     -->Ions
     -->Molecules

如果我实现简单的TextBlock,它的工作没有任何额外的库和本地源,只是简单的DataContext在代码背后和XAML:

<TextBox x:Name="TextBlock2" Text="{Binding MyItem.Name}"></TextBox>

MVVM中的WPF-TreeView绑定

使用HierarchicalDataTemplate作为TreeView.ItemTemplate

    <TreeView ItemsSource="{Binding MyItem.Items}">
            <TreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type local:ItemOfWorld }" ItemsSource = "{Binding Path=Items}">
                    <TextBlock Text="{Binding Path=Name}"/>
                </HierarchicalDataTemplate>
            </TreeView.Resources>
        </TreeView>