【问题标题】:WPF Combobox sorting, but initally no entry selected?WPF组合框排序,但最初没有选择条目?
【发布时间】:2020-05-25 22:58:09
【问题描述】:

我使用以下 XAML 为 WPF 组合框实现了排序:

<CollectionViewSource x:Key="SortedAreas" Source="{Binding AllAreas}">
  <CollectionViewSource.SortDescriptions>
    <scm:SortDescription PropertyName="Name"/>
  </CollectionViewSource.SortDescriptions>
</CollectionViewSource>

<ComboBox Name="PhotoLocationAreaComboBox"
          ItemsSource="{Binding Source={StaticResource SortedAreas}}"                
          DisplayMemberPath="Name">
</ComboBox>

这基本上可以正常工作 - 条目已排序,但有一个副作用:

  • 在我添加排序之前(直接绑定到作为 ObservableCollection 的 AllAreas,内部没有选择组合框条目(空)
  • 排序后,自动预选第一个排序的条目

知道如何通过排序恢复原始行为(未选择条目)吗?我试过 SelectedIndex = "0",但这没有帮助。

【问题讨论】:

标签: wpf sorting xaml combobox collectionviewsource


【解决方案1】:

IsSynchronizedWithCurrentItem 属性设置为false

<ComboBox Name="PhotoLocationAreaComboBox"
          ItemsSource="{Binding Source={StaticResource SortedAreas}}"   
          DisplayMemberPath="Name"
          IsSynchronizedWithCurrentItem="False">
</ComboBox>

【讨论】:

    猜你喜欢
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    • 2014-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    相关资源
    最近更新 更多