Ejemplo n.º 1
0
 /// <summary>
 /// 递归加载children
 /// </summary>
 /// <typeparam name="TSource"></typeparam>
 /// <typeparam name="TId"></typeparam>
 /// <param name="parent"></param>
 /// <param name="itemsArray"></param>
 /// <param name="captionSelector"></param>
 /// <param name="idSelector"></param>
 /// <param name="parentIdSelector"></param>
 /// <returns></returns>
 private TreeViewItemModel RecursiveAddChildren <TSource, TId>(TreeViewItemModel parent, IEnumerable <TSource> itemsArray, Func <TSource, string> captionSelector, Func <TSource, TId> idSelector, Func <TSource, TId> parentIdSelector, Func <TSource, bool> isCheckedSelector)
 {
     foreach (var item in itemsArray.Where(a => object.Equals(parent.Id, parentIdSelector(a))))
     {
         var tvi = new TreeViewItemModel();
         tvi.Caption   = captionSelector(item);
         tvi.Id        = idSelector(item);
         tvi.Tag       = item;
         tvi.TagType   = item.GetType();
         tvi.Parent    = parent;
         tvi.IsChecked = isCheckedSelector(item);
         parent.Children.Add(tvi);
         RecursiveAddChildren(tvi, itemsArray, captionSelector, idSelector, parentIdSelector, isCheckedSelector);
     }
     return(parent);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 设置数据源, 以及各个字段
        /// </summary>
        /// <typeparam name="TSource">数据源类型</typeparam>
        /// <typeparam name="TId">主键类型</typeparam>
        /// <param name="itemsArray">数据源列表</param>
        /// <param name="captionSelector">指定显示为Caption的属性</param>
        /// <param name="idSelector">指定主键属性</param>
        /// <param name="parentIdSelector">指定父项目主键属性</param>
        public void SetItemsSourceData <TSource, TId>(IEnumerable <TSource> itemsArray, Func <TSource, string> captionSelector, Func <TSource, TId> idSelector, Func <TSource, TId> parentIdSelector, Func <TSource, bool> isCheckedSelector)
            where TId : IEquatable <TId>
        {
            var list = new List <TreeViewItemModel>();

            foreach (var item in itemsArray.Where(a => object.Equals(default(TId), parentIdSelector(a))))
            {
                var tvi = new TreeViewItemModel();
                tvi.Caption   = captionSelector(item).ToString();
                tvi.Id        = idSelector(item);
                tvi.Tag       = item;
                tvi.TagType   = item.GetType();
                tvi.IsChecked = isCheckedSelector(item);
                list.Add(tvi);
                RecursiveAddChildren(tvi, itemsArray, captionSelector, idSelector, parentIdSelector, isCheckedSelector);
            }

            innerTree.ItemsSource = list;
            return;
        }