WPF - MVVM ???TreeView

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF - MVVM ???TreeView相关的知识,希望对你有一定的参考价值。

?????????dev   ted   val   cte   block   enter   new t   ??????   selected   

??????????????????OnPropertyChanged??????????????????????????????

private event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = "")
{
PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(name));
}

private int myVar;

public int MyProperty
{
get { return myVar; }
set { myVar = value; OnPropertyChanged("MyProperty"); }
}

TreeView???????????????????????????????????????

<TreeView ItemsSource="{Binding TVDataSource}" SelectedItemChanged="TreeView_SelectedItemChanged" >
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Nodes}">
                    <WrapPanel>
                        <TextBlock VerticalAlignment="Center" Text="{Binding Name}"/>
                    </WrapPanel>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>

??????TVDataSource????????????????????????????????????TreeViewDataModel????????????Name?????????Nodes?????????Nodes???????????????TreeViewNodeDataModel?????????

???????????????????????????TVDataSource????????????????????????????????????????????????????????????????????????????????????????????????????????????

?????????

public class TreeViewDataModel
    {

        public TreeViewDataModel()
        {
            Nodes = new ObservableCollection<TreeViewNodeDataModel>();
        }
        public string Name { get; set; }

        private ObservableCollection<TreeViewNodeDataModel> nodes;
        public ObservableCollection<TreeViewNodeDataModel> Nodes
        {
            get { return nodes; }
            set { nodes = value; OnPropertyChanged("Nodes"); }
        }

        private event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string name = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }



        public class TreeViewNodeDataModel
        {
            public string Name { get; set; }
        }
    }

??????

??????

?????????

private ObservableCollection<TreeViewDataModel> tVDataSource;
        public ObservableCollection<TreeViewDataModel> TVDataSource
        {
            get { return tVDataSource; } set {tVDataSource= value; OnPropertyChanged("TVDataSource"); } }

 

 

TVDataSource = new ObservableCollection<TreeViewDataModel>();
TreeViewDataModel tv = new TreeViewDataModel();
tv.Name = "A";
TreeViewNodeDataModel tvNode = new TreeViewNodeDataModel();
tvNode.Name = "a";
tv.Nodes.Add(tvNode);
TVDataSource.Add(tv);

 

以上是关于WPF - MVVM ???TreeView的主要内容,如果未能解决你的问题,请参考以下文章

WPF TreeView IsExpanded 绑定不上的问题

WPF - MVVM Treeview获取所选项目[重复]

WPF - MVVM ???TreeView

WPF中TreeView的使用

用户自定义TreeView控制双击冒泡事件WPF MVVM问题,怎么解决

WPF TreeView内部拖动项