Telerik RadTreeListView示例

        <telerik:RadTreeListView Margin="3"

                Name="tv" Grid.Row="2"

                telerik:StyleManager.Theme="Office_Silver" 

                AutoGenerateColumns="False" 

                IsFilteringAllowed="False" 

                HierarchyColumnIndex="1"

                RowIndicatorVisibility="Collapsed"

                SelectedCellsChanged="tv_SelectedCellsChanged"

                IsReadOnly="True">

            <telerik:RadTreeListView.ChildTableDefinitions>

                <telerik:TreeListViewTableDefinition ItemsSource="{Binding Items}" />

            </telerik:RadTreeListView.ChildTableDefinitions>

            <telerik:RadTreeListView.Columns>

                <telerik:GridViewSelectColumn />

                <telerik:GridViewDataColumn DataMemberBinding="{Binding ID}" Header="分类编号" IsVisible="False"/>

                <telerik:GridViewDataColumn DataMemberBinding="{Binding CategoryName}" Header="分类名称" Width="300" />

            </telerik:RadTreeListView.Columns>

        </telerik:RadTreeListView>
using System;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Ink;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using System.Collections.ObjectModel;



namespace SGSGHome.Views.File

{

    public class CatItem

    {

        public long ID { get; set; }

        public string CategoryName { get; set; }

        public long? ParentID { get; set; }

        public ObservableCollection<CatItem> Items { get; set; }

        public CatItem(long id, string catName, long? parentID)

        {

            this.ID = id;

            this.CategoryName = catName;

            this.ParentID = parentID;

            this.Items = new ObservableCollection<CatItem>();

        }

    }

}
        public IList<CoreService.FileCategory> CatList { get; set; }



        void client_GetFileCategoryListCompleted(object sender, CoreService.GetFileCategoryListCompletedEventArgs e)

        {

            //递归填充TreeListView

            this.CatList = e.Result;



            var root = CatList.First(x => x.ParentID == null);

            var rootItem = new CatItem(root.ID, root.CategoryName, null);

            AddChild(root.ID, rootItem);

            tv.ItemsSource = new ObservableCollection<CatItem>() { rootItem };

            tv.AutoExpandItems = true;

        }



        //递归方法

        void AddChild(long parentID, CatItem parentItem)

        {

            var filterList = CatList.Where(x => x.ParentID == parentID).ToList();



            foreach (var tt in filterList)

            {

                var childItem = new CatItem(tt.ID, tt.CategoryName, tt.ParentID);

                parentItem.Items.Add(childItem);

                var childrenList = CatList.Where(x => x.ParentID == tt.ID).ToList();

                if (childrenList.Count > 0)

                {

                    AddChild(tt.ID, childItem);

                }

            }

        }

 

你可能感兴趣的:(ListView)