WPF TreeView 绑定数据

Model 

  

  public  class TreeViewList
    {
        public TreeViewList()
        {

        }
        public string Menu_Name { get; set; }
///子菜单编号 public string Menu_Level { get; set; }
//主菜单编号("000"为主菜单,其他为子项) public string SORT_NO { get; set; } private List<TreeViewList> _Name; public List<TreeViewList> Childs { get { return _Name; } set { _Name = value; } } } }

ViewModel初始化数据

      
        private List<TreeViewList> _treeViews=new List<TreeViewList>();

        public List<TreeViewList> treeViews
        {
            get { return _treeViews; }
            set { _treeViews = value; }
        }
private void LoadMenus()
        {
            treeViews.Add(new TreeViewList() { Menu_Level = "001", SORT_NO = "000", Menu_Name = "1" });
            treeViews.Add(new TreeViewList() { Menu_Level = "001", SORT_NO = "000", Menu_Name = "2" });
            treeViews.Add(new TreeViewList() { Menu_Level = "002", SORT_NO = "000", Menu_Name = "2" });
            treeViews.Add(new TreeViewList() { Menu_Level = "002", SORT_NO = "000", Menu_Name = "3" });
            treeViews.Add(new TreeViewList() { Menu_Level = "003", SORT_NO = "004", Menu_Name = "3" });
            treeViews.Add(new TreeViewList() { Menu_Level = "003", SORT_NO = "004", Menu_Name = "5" });
            treeViews.Add(new TreeViewList() { Menu_Level = "004", SORT_NO = "000", Menu_Name = "5" });
            treeViews.Add(new TreeViewList() { Menu_Level = "004", SORT_NO = "003", Menu_Name = "5" });
            treeViews.Add(new TreeViewList() { Menu_Level = "005", SORT_NO = "003", Menu_Name = "6" });
            treeViews.Add(new TreeViewList() { Menu_Level = "005", SORT_NO = "000", Menu_Name = "6" });
           ViewLists = getTressData("000", treeViews);
        }

       private List<TreeViewList> getTressData(string data, List<TreeViewList> nodes)
        {
            List<TreeViewList> mainNodes = nodes.Where(x => x.SORT_NO == data).ToList<TreeViewList>();
            List<TreeViewList> otherNodes = nodes.Where(x => x.SORT_NO != data).ToList<TreeViewList>();
            foreach (TreeViewList item in mainNodes)
            {
                item.Childs = getTressData(item.Menu_Level, otherNodes);
            }
            return mainNodes;
        }

View绑定数据

       <TreeView Grid.Column="0" Grid.Row="0" x:Name="departmentTree"  PreviewMouseDown="DepartmentTree_PreviewMouseDown" ItemsSource="{Binding ViewLists}">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate  ItemsSource="{Binding Childs}">
                    <StackPanel  Orientation="Horizontal" Margin="0,2,0,2">
                        <TextBox IsEnabled="False"  Text="{Binding Menu_Name}" />
                    </StackPanel>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>

WPF TreeView 绑定数据

上一篇:C# 将Word转为PDF、XPS、Epub、RTF(基于Spire.Cloud.Word.SDK)


下一篇:.net webapi swagger读取不同项目实体模型的注释