首先我的样式操作:
文件名是:TreeViewStyle
我的界面配置:
然后 关于 treeview 的配置是
这个配置必须要 绑定ThisName textbolck 的text 显示 然后
ItemsSource="{Binding Children} 对应就是属性children数据
其中 属性为:
/// /// 栏目表 /// public class MainItemInfos { /// /// /// [Description("")] public int Id { get; set; } /// /// 编号 /// [Description("编号")] public string ThisNo { get; set; } /// /// 父id /// [Description("父id")] public int PId { get; set; } /// /// 当前地址 /// [Description("当前地址")] public string ThitPath { get; set; } /// /// 当前名称 /// [Description("当前名称")] public string ThisName { get; set; } /// /// 是否启用 /// [Description("是否启用")] public bool IsUsed { get; set; } /// /// 数据类型(0,首页,1 栏目,3根栏目) /// [Description("数据类型(0 顶级页,1首页,2 栏目,3根栏目)")] public EItemType PathType { get; set; } /// /// /// [Description("")] public DateTime CreateDate { get; set; } /// /// /// [Description("")] public DateTime Updated { get; set; } /// /// 当前 /// public MainItemInfos ParentInfo { get; set; } /// /// 当前所有子数据 /// public ObservableCollection Children { get; set; } }
另外 从数据库获取来后的list操作
/// /// 获取树结构下所有数据 /// /// /// /// public void GetTreeNodeModel(MainItemInfos ThisInfo, List ThisListAll, ref List RetureInfolist) { List ListOut = new List(); IEnumerable ListThisMains = ThisListAll.Where(x => x.PId == ThisInfo.Id); if (ListThisMains != null && ListThisMains.Count() > 0) { ThisInfo.Children = new System.Collections.ObjectModel.ObservableCollection(ListThisMains); RetureInfolist.Add(ThisInfo); foreach (MainItemInfos OneInfo in ThisInfo.Children) { OneInfo.ParentInfo = ThisInfo; GetChildren(OneInfo, ThisListAll); //GetTreeNodeModel(OneInfo, ThisListAll, ref RetureInfolist); } } } /// /// 子目录下的文件(这样只有一个List可以只有一个树结构)一一更新 /// /// /// public void GetChildren(MainItemInfos ThisModel,List ListAll) { if (ListAll != null ) { IEnumerable ListThisMains = ListAll.Where(x => x.PId == ThisModel.Id); if (ListThisMains!=null && ListThisMains.Count() > 0) { ThisModel.Children = new System.Collections.ObjectModel.ObservableCollection(ListThisMains) ; foreach (MainItemInfos OneItem in ThisModel.Children) { GetChildren(OneItem,ListAll); } } } } /// /// 刷新树结构 /// public void ReflashNodes() { List ListDB = new DAL.DALMainItemInfos().QueryList().ToList(); MainItemInfos ThisZongInfos = ListDB.Where(x => x.PathType == 0).FirstOrDefault(); ListDB.Remove(ThisZongInfos); List ListBinding = new List(); GetTreeNodeModel(ThisZongInfos, ListDB, ref ListBinding); if (ListBinding != null) { MainTree.ItemsSource = ListBinding; } }