WPF 排序子集合
本文关键字:子集合 排序 WPF | 更新日期: 2023-09-27 18:35:54
我有一个项目集合,我想在 ItemsControl 中显示这些项目。然后,这些项目中的每一个都将有一个子项目的集合,这些子项目应水平显示在每个项目中。我的问题是如何在 XAML 中对子项进行排序?我通常会使用CollectionViewSource,但在这种情况下无法使其工作。下面是一个简单的示例。在这种情况下,somedata 是我的项目集合(字符串),项目是字符的集合。我只想修改此示例,以便对每一行的所有字符进行排序。
把这是一个窗口的构造函数:
string[] somedata = new string[] { "afkhsdfgjh", "fsdkgjhsdfjh", "sdfjhdfsjh" };
mainList.ItemsSource = somedata;
这作为 XAML(在窗口标记内)
<ItemsControl Name="mainList">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Margin" Value="0,0,5,0"></Setter>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
您也可以将 CollectionViewSource 用于子项。下面是示例视图模型:
public class Item : ViewModel
{
public String Name
{
get { return name; }
set
{
if (name != value)
{
name = value;
OnPropertyChanged("Name");
}
}
}
private String name;
public ObservableCollection<String> SubItems
{
get
{
return subItems ?? (subItems = new ObservableCollection<String>());
}
}
private ObservableCollection<String> subItems;
}
。和标记:
<ListBox ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<StackPanel.Resources>
<CollectionViewSource Source="{Binding SubItems}" x:Key="subItemsViewSource">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription Direction="Ascending" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</StackPanel.Resources>
<TextBlock Text="{Binding Name}" />
<ListBox ItemsSource="{Binding Source={StaticResource subItemsViewSource}}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
在代码隐藏中,使用一些数据初始化数据上下文:
public MainWindow()
{
InitializeComponent();
DataContext = new ObservableCollection<Item>
{
new Item
{
Name = "John",
SubItems =
{
"Mary", "Peter", "James"
},
},
new Item
{
Name = "Homer",
SubItems =
{
"Lisa", "Bart", "Marge"
},
}
};
}
诀窍是将描述 CollectionViewSource 的资源放入主列表项的布局控件资源中。
可以在内部 ItemsControl 上使用值转换器。 转换器将简单地返回排序后的数组。
public class ArraySortConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null && value is string)
{
return new string((value as string).OrderBy(ch => ch).ToArray());
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException("Cannot convert back");
}
}
我想您还必须添加一个 ItemsControl.ItemTemplate 才能实际使用转换器,如下所示:
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentPresenter Content="{Binding Converter={StaticResource ArraySorter}}" />
</DataTemplate>
</ItemsControl.ItemTemplate>