添加新记录后,我想在纯 WPF 中对可编辑的组合框进行排序。经过一些研究,我发现了一些使用 a 的提示CollectionViewSource.SortDescription
,但它对我不起作用。我究竟做错了什么?使用我的项目的 DataContextDataTemplate
工作正常,但DataTemplate
与资源部分之间的绑定以对我的条目列表进行排序却没有。
我的 XAML 部分
<DataTemplate x:Key="Document">
<DataTemplate.Resources>
<CollectionViewSource x:Key="SortedLabels" Source="{Binding Parent.Labels}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Items"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</DataTemplate.Resources>
<Grid>
...
<ComboBox Name="cbLabel"
ItemsSource="{Binding Source={StaticResource SortedLabels}}"
IsEditable="True"
LostFocus="cbLabel_LostFocus"
KeyUp="cbLabel_KeyUp"
Visibility="{Binding Path=IsUndiscovered, Converter={StaticResource ResourceKey=BooleanToVisibilityConverter}}"/>
...
</Grid>
</DataTemplate>
编辑
集合Parent.Labels
是类型ObservableCollection<String>
。