Gaz*_*yer 5 c# wpf combobox selecteditem collectionviewsource
我有一堆组合,它们都共享相同的可用选项。这些选择在从我的 ViewModel 公开的集合中提供。一切都很好,花花公子。
我现在希望对这些选择进行排序,因此我决定ICollectionView从我的 ViewModel公开 an而不是我通常的ReadonlyObservableCollection<T>,并在我的 ViewModel 中对集合视图进行排序。
class EditStuffViewModel : ViewModelBase
{
public EditStuffViewModel (ObservableCollection<Choice> choices)
{
Choices = new CollectionViewSource() { Source = choices }.View;
Choices.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
}
public ICollectionView Choices
{
get;
private set;
}
//snip other properties
}
Run Code Online (Sandbox Code Playgroud)
这一切都很好,只是现在我所有的组合现在都同步了他们的选择。
这不是我想要的。我希望共享选择,但选择要与其正常绑定。我想我知道我的 CollectionView 正在跟踪选择,但我认为这是为每个控件选择的行为。
我已经尝试明确设置IsSynchronizedWithCurrentItem="False"我的组合,这成功地将它们解耦,但是我的绑定SelectedItem从未在组合中选择(调用了 ViewModel 的绑定 getter,但从未选择结果)。选择一个项目似乎可以正确更新我的 ViewModel 的 setter。
我显然错过了 CollectionView 应该如何工作的基本知识。任何人都可以启发我吗?
编辑:我不好,这确实适用于IsSynchronizedWithCurrentItem="False". 详情请看我的回答。
干杯。
很抱歉浪费大家的时间,但是设置IsSynchronizedWithCurrentItem="False"确实有效。我还添加了一个过滤器以及排序,默认选择的值不在过滤的项目列表中。哎呀。
至于为什么我需要明确地关闭 IsSynchronizedWithCurrentItem 当我通常不会在标准集合上关闭时,MSDN上的灯亮了
如果 SelectedItem 始终与 ItemCollection 中的当前项目同步,则为true;如果 SelectedItem 从未与当前项目同步,则为false;仅当 Selector 使用 CollectionView 时 SelectedItem 才与当前项同步。默认值为Nothing。
因此,换句话说,如果您CollectionView在普通集合上显式使用而不是使用默认视图,则会获得选择同步。
| 归档时间: |
|
| 查看次数: |
4756 次 |
| 最近记录: |