MVVM中的WPF-TreeView DataBinding

时间:2014-06-19 10:50:14

标签: c# wpf xaml mvvm treeview

我只是尝试用带有DataBinding的WPF-TreeView实现简单的MVVM模型。但是我发现了一些问题并希望,你们中的某个人可以帮助我。

我有模特,我在那里定义自己的课程:

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

}

最后,我有一个View with TreeView工具箱,带有简单的代码隐藏:

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

我的问题是:如何在不使用local-sources的情况下在TreeView中显示我的层次结构?

-MyWorld
     -->Ions
     -->Molecules

如果我实现简单的TextBlock,它可以在没有任何额外的库和本地源的情况下工作,只需在代码隐藏和XAML中使用简单的DataContext:

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

1 个答案:

答案 0 :(得分:0)

使用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>