0

嗨,我对分层模板有疑问。我编写了我的自定义树视图项目并使用分层模板进行绑定。但是我的树视图没有更新。我引用了该网站http://social.msdn.microsoft.com/Forums/vstudio/en-US/feac927d-b73c-4361-82f4-cd0a3f055ad0/treeview-binding-and-hierarchicaldatatemplate

我的 xml

    <Window.DataContext>
        <vm:ViewModel></vm:ViewModel>
    </Window.DataContext>
    <Grid>
        <TreeView  Name="FT" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" Grid.Row="1" Grid.ColumnSpan="4" ItemsSource="{Binding RootNode}"  >
            <TreeView.Resources>
                <DataTemplate DataType="{x:Type vm:FrthChild}">
                    <TextBlock Text="{Binding DisplayName}"/>
                </DataTemplate>

                <HierarchicalDataTemplate DataType="{x:Type vm:ThrdChild}"
         ItemsSource="{Binding FrthChilds}">
                    <TextBlock Text="{Binding DisplayName}"/>
                </HierarchicalDataTemplate>

                <HierarchicalDataTemplate
         DataType="{x:Type vm:SecndChild}"
        ItemsSource="{Binding ThrdChilds}">
                    <TextBlock Text="{Binding DisplayName}"/>
                </HierarchicalDataTemplate>

                <HierarchicalDataTemplate
         DataType="{x:Type vm:FstChild}"
         ItemsSource="{Binding SecndChilds}">
                    <TextBlock Text="{Binding DisplayName}"/>
                </HierarchicalDataTemplate>

                <HierarchicalDataTemplate  DataType="{x:Type vm:ROOT}" ItemsSource="{Binding FstChilds}">
                    <TextBlock Text="{Binding DisplayName}"/>
                </HierarchicalDataTemplate>
            </TreeView.Resources>
        </TreeView>
    </Grid>
</Window>

我的视图模型类

    namespace WpfApplication1 : INotifyPropertyChanged
    {
        public class ViewModel
        {
            public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, e);
        }
            public ViewModel()
            {
                RootNode = new ROOT();
            }
            public ROOT RootNode
            {
                get;
                set;
            }
        }
    }

我的树视图项目类

namespace WpfApplication1
{
    public class NodeTypes
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, e);
        }
        public string DisplayName
        { get; set; }
    }
    public class ROOT : NodeTypes
    {
        public ROOT()
        {
            FstChilds = new ObservableCollection<FstChild>();
        }
        public ObservableCollection<FstChild> FstChilds { get; set; }
    }
    public class FstChild : NodeTypes
    {
        public FstChild()
        {
            SecndChilds = new ObservableCollection<SecndChild>();
        }
        public ObservableCollection<SecndChild> SecndChilds { get; set; }
    }
    public class SecndChild : NodeTypes
    {
        public SecndChild()
        {
            ThrdChilds = new ObservableCollection<ThrdChild>();
        }
        public ObservableCollection<ThrdChild> ThrdChilds { get; set; }
    }
    public class ThrdChild : NodeTypes
    {
        public ThrdChild()
        {
            FrthChilds = new ObservableCollection<FrthChild>();
        }
        public ObservableCollection<FrthChild> FrthChilds { get; set; }
    }
    public class FrthChild : NodeTypes
    {

    }
}

我在我的 xaml.cs 中添加了一些项目...我这样做是为了方便编写测试应用程序。我稍后将使用 ICommand 实现它。

Xaml.cs

        namespace WpfApplication1
        {
            /// <summary>
            /// Interaction logic for MainWindow.xaml
            /// </summary>
            public partial class MainWindow : Window
            {
                public MainWindow()
                {
                    InitializeComponent();
                    ((ViewModel)this.DataContext).RootNode = new ROOT();
                    ((ViewModel)this.DataContext).RootNode.DisplayName = "sdsds";
                    ((ViewModel)this.DataContext).OnPropertyChanged(new PropertyChangedEventArgs("RootNode"));
                    ((ViewModel)this.DataContext).RootNode.OnPropertyChanged(new PropertyChangedEventArgs("DisplayName"));
                    ((ViewModel)this.DataContext).RootNode.FstChilds.Add(new FstChild());
                    ((ViewModel)this.DataContext).RootNode.FstChilds[0].DisplayName = "sered";
                    ((ViewModel)this.DataContext).RootNode.FstChilds[0].OnPropertyChanged(new PropertyChangedEventArgs("DisplayName"));
                    ((ViewModel)this.DataContext).RootNode.FstChilds[0].SecndChilds.Add(new SecndChild());
                    ((ViewModel)this.DataContext).RootNode.FstChilds[0].SecndChilds[0].DisplayName = "dsdsd";
                    ((ViewModel)this.DataContext).RootNode.FstChilds[0].SecndChilds[0].OnPropertyChanged(new PropertyChangedEventArgs("DisplayName"));
                    ((ViewModel)this.DataContext).RootNode.FstChilds[0].SecndChilds[0].ThrdChilds.Add(new ThrdChild());
                    ((ViewModel)this.DataContext).RootNode.FstChilds[0].SecndChilds[0].ThrdChilds[0].DisplayName = "dsds";
                    ((ViewModel)this.DataContext).RootNode.FstChilds[0].SecndChilds[0].ThrdChilds[0].OnPropertyChanged(new PropertyChangedEventArgs("DisplayName"));
                    ((ViewModel)this.DataContext).RootNode.FstChilds[0].SecndChilds[0].ThrdChilds[0].FrthChilds.Add(new FrthChild());
                    ((ViewModel)this.DataContext).RootNode.FstChilds[0].SecndChilds[0].ThrdChilds[0].FrthChilds[0].DisplayName = "dsds";
                    ((ViewModel)this.DataContext).RootNode.FstChilds[0].SecndChilds[0].ThrdChilds[0].FrthChilds[0].OnPropertyChanged(new PropertyChangedEventArgs("DisplayName"));

                }
            }
        }

任何人都可以帮助我吗?

4

2 回答 2

1

RootNode 必须是 IEnumerable 所以我改变了你的 ViewModel:

    public class ViewModel
{
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, e);
    }
    public ViewModel()
    {
        RootNode = new ObservableCollection<ROOT>() { new ROOT() };
    }
    public ObservableCollection<ROOT> RootNode
    {
        get;
        set;
    }
}

和 MainWindow ctor:

        public MainWindow()
    {
        InitializeComponent();
        // ((ViewModel)this.DataContext).RootNode = new ROOT();
        ((ViewModel)this.DataContext).RootNode[0].DisplayName = "sdsds";
        ((ViewModel)this.DataContext).OnPropertyChanged(new PropertyChangedEventArgs("RootNode"));
        ((ViewModel)this.DataContext).RootNode[0].OnPropertyChanged(new PropertyChangedEventArgs("DisplayName"));
        ((ViewModel)this.DataContext).RootNode[0].FstChilds.Add(new FstChild());
        ((ViewModel)this.DataContext).RootNode[0].FstChilds[0].DisplayName = "sered";
        ((ViewModel)this.DataContext).RootNode[0].FstChilds[0].OnPropertyChanged(new PropertyChangedEventArgs("DisplayName"));
        ((ViewModel)this.DataContext).RootNode[0].FstChilds[0].SecndChilds.Add(new SecndChild());
        ((ViewModel)this.DataContext).RootNode[0].FstChilds[0].SecndChilds[0].DisplayName = "dsdsd";
        ((ViewModel)this.DataContext).RootNode[0].FstChilds[0].SecndChilds[0].OnPropertyChanged(new PropertyChangedEventArgs("DisplayName"));
        ((ViewModel)this.DataContext).RootNode[0].FstChilds[0].SecndChilds[0].ThrdChilds.Add(new ThrdChild());
        ((ViewModel)this.DataContext).RootNode[0].FstChilds[0].SecndChilds[0].ThrdChilds[0].DisplayName = "dsds";
        ((ViewModel)this.DataContext).RootNode[0].FstChilds[0].SecndChilds[0].ThrdChilds[0].OnPropertyChanged(new PropertyChangedEventArgs("DisplayName"));
        ((ViewModel)this.DataContext).RootNode[0].FstChilds[0].SecndChilds[0].ThrdChilds[0].FrthChilds.Add(new FrthChild());
        ((ViewModel)this.DataContext).RootNode[0].FstChilds[0].SecndChilds[0].ThrdChilds[0].FrthChilds[0].DisplayName = "dsds";
        ((ViewModel)this.DataContext).RootNode[0].FstChilds[0].SecndChilds[0].ThrdChilds[0].FrthChilds[0].OnPropertyChanged(new PropertyChangedEventArgs("DisplayName"));

    }

一切都显示出来了。

编辑: 关于您的最后一条评论,您可以尝试使用 ItemContainer 样式

        <TreeView.ItemContainerStyle>
            <Style TargetType="TreeViewItem">
                <Setter Property="IsSelected" Value="{Binding YourProperty}"></Setter>
            </Style>
        </TreeView.ItemContainerStyle>
于 2013-09-03T07:07:18.953 回答
1

您还没有为您的属性引发 propertychanged 事件。你应该这样做:

           private ROOT rootNode;
            public ROOT RootNode
            {
                get
                  {
                    return _rootNode;
                  }
                set
                   {
                      _rootNode = value;
                       OnPropertyChanged(new PropertyChangedEventArgs("RootNode"));
                     }
            }

同样,对于所有类从属性设置器调用 PropertyChanged 事件。

谢谢

于 2013-09-03T06:50:09.700 回答