2

I am using a CollectionViewSource and have set up the CollectionViewSource.SortDescription property in xaml for sorting the collection on a particular property.

However, now I have a case wherein if certain condition is true, or collection is of particular type, I don't need to apply the sorting, but rather to just bind the collection as it is from the view model.

I don't want to move the sorting out of XAML to view model, since it complicates the matter. I want to leave sorting to CollectionViewSource.SortDescription, however want to know if there is a way to turn it off based on some flag. E.g. I can expose a property IgnoreSort in my view model and just somehow consume it to turn the CollecitonViewSource sorting off.

Following is the xaml code-

Resource:

<UserControl.Resources>
    <CollectionViewSource x:Key="PeopleItemsSource" Source="{Binding Department.ActivePeople}">
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="DisplaySortOrder" />
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>
</UserControl.Resources>

And here is the control using the resource declared above

 <ItemsControl x:Name="peopleitems"
            Grid.Row="1"
            ItemsSource="{Binding Source={StaticResource PeopleItemsSource}}"/>

Note: Item template is not added here to simplify the xaml.

AbSharp
  • 119
  • 2
  • 6
  • 20

2 Answers2

1

Here's a small example I wrote, I don't think you'll have issues adapting it to your project, it uses DataTriggers to conditionally choose the itemssource, it doesn't use code behind, the code behind you see here is just to setup the datacontext, the items collection etc. Copy paste this code into a new WPF project and see it working

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525"
    xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
    >
<Window.Resources>
    <CollectionViewSource x:Key="colSrc" Source="{Binding MyList}">
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription/>
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>
    <Style TargetType="ItemsControl" x:Key="ic">
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsSorted}" Value="True">
                <Setter Property="ItemsSource" Value="{Binding Source={StaticResource ResourceKey=colSrc}}"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding IsSorted}" Value="False">
                <Setter Property="ItemsSource" Value="{Binding MyList}"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<Grid>
    <StackPanel>
        <CheckBox IsChecked="{Binding IsSorted}"/>
        <ItemsControl Grid.Row="1" Style="{StaticResource ic}"/>
    </StackPanel>
</Grid>

MainWindow.xaml.cs

namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window,INotifyPropertyChanged
{
    private bool isSorted;
    public bool IsSorted
    {
        get
        {
            return isSorted;
        }
        set
        {
            isSorted = value;
            OnPropertyChanged("IsSorted");
        }
    }
    private ObservableCollection<string> myList;

    public event PropertyChangedEventHandler PropertyChanged;

    public ObservableCollection<string> MyList
    {
        get
        {
            return myList;
        }
        set
        {
            myList = value;
            this.OnPropertyChanged("MyList");
        }
    }
    public MainWindow()
    {
        InitializeComponent();

        this.MyList = new ObservableCollection<string>() {
            "C",
            "B",
            "A"
        };
        this.DataContext = this;

    }
    private void OnPropertyChanged(string p)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(p));
        }
    }
}
}
The One
  • 4,560
  • 5
  • 36
  • 52
-1

you must do it by code - add or remove the SortProperties

internal void Sort(PropertyModel model)
    {
        PropertyViewModel prop = SortProperties.Find(p => p.Data == model);

        IEnumerable<SortDescription> result =
            Books.SortDescriptions.Cast<SortDescription>().Where(p => p.PropertyName == prop.Data.FullName);

        if (result != null && result.Count() == 1)
        {
            Books.SortDescriptions.Remove(result.First());
        }
        else
        {
            Books.SortDescriptions.Add(new SortDescription(prop.Data.FullName, ListSortDirection.Ascending));
        }

        RaisePropertyChanged("Books");
    }
GCamel
  • 612
  • 4
  • 8