我有一个 ObservableCollection 项目,我想同时在两个控件中呈现:一个用于编辑当前选择的属性以及添加和删除项目,另一个用于显示整个集合并突出显示当前选择。
为此,我创建了一个 CollectionViewSource 绑定到我的项目,并在我的控件中绑定到该项目:
<UserControl>
<UserControl.Resources>
<CollectionViewSource x:Key="MyCollectionViewSource" Source="{Binding MyItems}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="MyProperty" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</UserControl.Resources>
<Grid>
<my:PropertiesControl TheItems="{Binding Source={StaticResource MyCollectionViewSource}}" >
<my:DisplayControl TheItems="{Binding Source={StaticResource MyCollectionViewSource}}" >
</Grid>
</UserControl>
问题在于,在 DisplayControl 中,我想以无法通过 XAML 指定的特殊方式排列项目:项目的放置不仅取决于其自身的属性,还取决于集合中其他项目的属性。因此,每当将项目添加到集合中或从集合中删除时,或者当集合中某个项目的属性发生更改时,我都需要执行一些重新定位代码。
我想知道,推荐的解决方法是什么?受 ItemsControl.ItemsSource 的启发,我正在考虑将 TheItems 属性设为 IEnumerable 类型,但我无法弄清楚如何继续。
任何输入将不胜感激!