Issues when customize sorting for TreeListView

3 posts, 0 answers
  1. Jane
    Jane avatar
    4 posts
    Member since:
    May 2011

    Posted 13 Jun 2011 Link to this post

    I met issues when I try to customize sorting for TreeListView control. Could you please give me some help on these issues? In general, I met 3 major issues:
    Issue 1: Click one column header(e.g. "Name" column) -> the sorting is applyed and the small triangle -> Modifiy the name of Item1 as Item4 -> Item4 is not move to the bottom of tree list view.
    Issue 2: Click one column header(e.g. "Name" column) -> the sorting is applyed and the small triangle -> Expand one node in tree list view -> the small triangle is not appeared anymore.
    Issue 3: First expend all nodes in TreeListView -> Click one column header(e.g. "Name" column) -> All expend nodes are collapsed.

    Following are all the codes of my prototype:
    ViewModel.cs:
    public class ViewModel
    {
       public ViewModel()
       {
          Items = new ObservableCollection<ItemVM>();
          InitItems();
       }
       public ObservableCollection<ItemVM> Items { get; set; }
       private void InitItems()
       {
          for (int i = 1; i < 4; i++ )
          {
             ItemVM item = new ItemVM("Item" + i);
             Items.Add(item);
          }
          int index = 1;
          foreach (ItemVM itemvm in Items)
          {
             for (int i = 1; i < 4; i++ )
             {
                ItemVM item = new ItemVM(String.Format("Item{0}-Object{1}",index,i));
                itemvm.Items.Add(item);
             }
             index++;
          }
       }
    }
    public class ItemVM
    {
       public ItemVM(string name)
       {
          this.Name = name;
          Items = new ObservableCollection<ItemVM>();
       }
       public string Name
       {
          get
          {
             return m_cached_name;
          }
          set
          {
             if (m_cached_name != value)
             {
                m_cached_name = value;
             }
          }
       }
       public ObservableCollection<ItemVM> Items { get; set; }
       private String m_cached_name;
    }

    MainWindow.xaml
    <Window xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"  x:Class="RedTreeView_Demo.MainWindow"
            xmlns:vm="clr-namespace:RedTreeView_Demo"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <vm:ViewModel x:Key="view_model"/>
        </Window.Resources>
        <Grid>
            <telerik:RadTreeListView Name="RadTreeListView1"  
                                     ItemsSource="{Binding Source={StaticResource view_model}, Path=Items}"
                                     Sorting="RadTreeListView1_Sorting">
                <telerik:RadTreeListView.ChildTableDefinitions>
                    <telerik:TreeListViewTableDefinition ItemsSource="{Binding Items}"/>
                </telerik:RadTreeListView.ChildTableDefinitions>
                <telerik:RadTreeListView.Columns>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}" Header="Name" />
                </telerik:RadTreeListView.Columns>
            </telerik:RadTreeListView>
        </Grid>
    </Window>

    MainWindow.xaml.cs
    namespace RedTreeView_Demo
    {
       /// <summary>
       /// Interaction logic for MainWindow.xaml
       /// </summary>
       public partial class MainWindow : Window
       {
          public MainWindow()
          {
             InitializeComponent();
          }
      
          private void RadTreeListView1_Sorting(object sender, Telerik.Windows.Controls.GridViewSortingEventArgs e)
          {
             //Gets the value of the ItemSource property
             IEnumerable<ItemVM> ItemVMs = e.DataControl.ItemsSource as IEnumerable<ItemVM>;
             if (ItemVMs == null)
             {
                e.Cancel = true;
                return;
             }
      
             //If the sorting state is none or descending, sort the items ascending
             if (e.OldSortingState == SortingState.None || e.OldSortingState == SortingState.Descending)
             {
                e.NewSortingState = SortingState.Ascending;
                ItemVMs = ItemVMs.OrderBy(item => item.GetType().GetProperty(e.Column.UniqueName).GetValue(item, null));
                foreach (ItemVM itemVM in ItemVMs)
                {
                   IEnumerable<ItemVM> ivms = itemVM.Items as IEnumerable<ItemVM>;
                   ivms = ivms.OrderBy(item => item.GetType().GetProperty(e.Column.UniqueName).GetValue(item, null));
                   itemVM.Items = new ObservableCollection<ItemVM>();
                   foreach (ItemVM item in ivms)
                   {
      
                      itemVM.Items.Add(item);
                   }
                }
             }
             //If the sorting state is ascending, sort the items descending
             else if (e.OldSortingState == SortingState.Ascending)
             {
                e.NewSortingState = SortingState.Descending;
                ItemVMs = ItemVMs.OrderByDescending(item => item.GetType().GetProperty(e.Column.UniqueName).GetValue(item, null));
                foreach (ItemVM itemVM in ItemVMs)
                {
                   IEnumerable<ItemVM> ivms = itemVM.Items as IEnumerable<ItemVM>;
                   ivms = ivms.OrderByDescending(item => item.GetType().GetProperty(e.Column.UniqueName).GetValue(item, null));
                   itemVM.Items = new ObservableCollection<ItemVM>();
                   foreach (ItemVM item in ivms)
                   {
                      itemVM.Items.Add(item);
                   }
                }
             }
      
             //Set the sorted collection as source of the RadTreeListView
             e.DataControl.ItemsSource = ItemVMs.ToList();
             e.Cancel = true;
          }
       }
    }
  2. Jane
    Jane avatar
    4 posts
    Member since:
    May 2011

    Posted 14 Jun 2011 Link to this post

    I am using version 2011.1.419.35.
    Highly appreciate for any help.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1127 posts

    Posted 15 Jun 2011 Link to this post

    Hello Jane,

    I have just posted an answer to your inquiry in the other thread, you have opened. In order to achieve a fruitful and consistent dialogue, I would ask you to carry out any further communication there. Thank you.

    Best wishes,
    Ivan Ivanov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top