Home Forums WPF controls Xceed Toolkit Plus for WPF ListBox Sorting by MVVM

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • Rosha
    Participant
    Post count: 1
    #44797 |

    How can I Sorting Xceed ListBox by MVVM ? I don’t have problem with standard ListBox, The following code works with standard ListBox.

        public class CollectionViewBehavior : Behavior<ItemsControl>
        {
            public IEnumerable ItemsSource
            {
                get { return (IEnumerable)GetValue(ItemsSourceProperty); }
                set { SetValue(ItemsSourceProperty, value); }
            }
            public static readonly DependencyProperty ItemsSourceProperty =
                DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(CollectionViewBehavior), new PropertyMetadata(null, (d, e) => ((CollectionViewBehavior)d).OnItemsSourceChanged()));
    
            CollectionViewSource source;
            public ICommand SortAscendingCommand { get; private set; }
            public ICommand SortDescendingCommand { get; private set; }
            public ICommand SortResetCommand { get; private set; }
    
            public CollectionViewBehavior()
            {
                source = new CollectionViewSource();
                SortAscendingCommand = new DelegateCommand<string>(x => {
                    source.SortDescriptions.Clear();
                    source.SortDescriptions.Add(new SortDescription(x, ListSortDirection.Ascending));
                });
                SortDescendingCommand = new DelegateCommand<string>(x => {
                    source.SortDescriptions.Clear();
                    source.SortDescriptions.Add(new SortDescription(x, ListSortDirection.Descending));
                });
                SortResetCommand = new DelegateCommand<string>(x =>
                {
                    source.SortDescriptions.Clear();
                });
            }
    
            protected override void OnAttached()
            {
                base.OnAttached();
                AssociatedObject.SetBinding(ItemsControl.ItemsSourceProperty, new Binding() { Source = source, Mode = BindingMode.OneWay });
            }
            protected override void OnDetaching()
            {
                base.OnDetaching();
                AssociatedObject.ClearValue(ItemsControl.ItemsSourceProperty);
            }
            void OnItemsSourceChanged()
            {
                source.Source = ItemsSource;
            }
        }
    Fawzi [Xceed]
    Member
    Post count: 722

    Hi,

    Can you give me more details regarding what do you mean but: sorting is not working. Could you send me a simple stand alone sample project which reproduces the issue so that I could have a closer look. You may send it to: support@xceed.com

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.