【问题标题】:Using multiple sort descriptions with CollectionViewSource?对 CollectionViewSource 使用多个排序描述?
【发布时间】:2017-05-11 01:40:21
【问题描述】:
我有一个定义了排序描述的 CollectionViewSource,但是我想根据用户可以选择的单独下拉列表更改这些排序描述。理想情况下,我想在没有代码隐藏的情况下实现所有这些。
<CollectionViewSource x:Key="SortedPeople" Source="{Binding People}">
<!--sort by first name then last name -->
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="FirstName"/>
<scm:SortDescription PropertyName="LastName"/>
</CollectionViewSource.SortDescriptions>
<!-- another way to do this?
<!--sort by last name then first name -->
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="LastName"/>
<scm:SortDescription PropertyName="FirstName"/>
</CollectionViewSource.SortDescriptions>-->
</CollectionViewSource>
(...xaml for drop down box which sets which set of sort descriptions to use...)
有可能吗?
【问题讨论】:
标签:
c#
wpf
xaml
sorting
collectionviewsource
【解决方案1】:
这是我的做法。
首先创建单独的CollectionViewSource 定义
<UserControl.Resources>
<CollectionViewSource x:Key="SortedFirstName" Source="{Binding People}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="FirstName"/>
<scm:SortDescription PropertyName="LastName"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
<CollectionViewSource x:Key="SortedLastName" Source="{Binding People}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="LastName"/>
<scm:SortDescription PropertyName="FirstName"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</UserControl.Resources>
然后,假设您有一个 ItemsControl,并且您想设置它的样式以使用不同的排序方法。您可以使用 DataTriggers:
<ItemsControl.Style>
<Style TargetType="{x:Type ItemsControl}">
<Setter Property="ItemsSource" Value="{Binding Source={StaticResource SortedLastName}}"/><!-- default value -->
<Style.Triggers>
<DataTrigger Binding="{Binding Path=SortBy}" Value="FirstName">
<Setter Property="ItemsSource" Value="{Binding Source={StaticResource SortedFirstName}}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ItemsControl.Style>
请注意,在上面的代码中,我绑定到了一个名为 SortBy 的属性。这是我的 viewModel 中的一个属性,它是一个字符串。当该属性更改时,我使用 DataTrigger 更改我正在使用的 CollectionViewSource。
【解决方案2】:
假设您有一个包含多个排序参数的组合框。
<ComboBox ItemsSource="{Binding SortByItemList}"
SelectedItem="{Binding SelectedSortByItem,Mode=TwoWay}"/>
在视图模型中,放这个:
public string SelectedSortByItem
{
get
{
return _selectedSortByItem;
}
set
{
_selectedSortByItem = value;
OnSelectedSortByItemChanged(People, SelectedSortByItem);
RaisePropertyChanged();
}
}
public IList<People> OnSelectedSortByItemChanged(IList<People> items, string sortBy)
{
if (items == null || !items.Any())
{
return items;
}
var itemsToSort = items.ToList();
if (sortBy == "FirstName")
{
return Sort(itemsToSort, "FirstName");
}
else if (sortBy == "LastName")
{
return Sort(itemsToSort, "LastName");
}
// put as many properties as you want here
}
private static List<People> Sort(
List<People> itemsToSort,
string propertyPath,
ListSortDirection sortDirection = ListSortDirection.Ascending)
{
// put your sorting logic here.
}
希望这会有所帮助。