0

添加新记录后,我想在纯 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>

4

1 回答 1

0

很难说,因为您没有显示要排序的集合是什么,但我的猜测是 Parent.Labels 中的每个对象都没有 Items 属性。PropertyName 指的是列表中单个对象的属性,它将查看并尝试排序。我猜您想要更多类似于“名称”或其他属性的东西。

于 2012-10-26T13:34:40.833 回答