I have an ObservableCollection of items that I want to present in two controls simultaneously: One for editing properties for the current selection and adding and deleting items, and one for displaying the entire collection with the current selection highlighted.
我有一个要在两个控件中同时显示的项的观察汇总:一个用于编辑当前选择的属性并添加和删除项,另一个用于显示当前选择突出显示的整个集合。
To this end I create a CollectionViewSource that binds to my items, and bind to that in my controls:
为此,我创建了一个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>
The problem is that in DisplayControl I want to arrange the items in a special way that can not be specified through XAML: The placement of an item depends not only on its own properties but also on that of the other items in the collection. Thus I need to execute some repositioning code whenever items are added to or deleted from the collection, or when the properties of one of the items in the collection changes.
问题是,在DisplayControl中,我希望以一种特殊的方式排列这些项,这种方式不能通过XAML来指定:项的位置不仅取决于它自己的属性,而且还取决于集合中其他项的属性。因此,每当向集合中添加或删除项,或者当集合中某一项的属性发生变化时,我都需要执行一些重新定位代码。
I was wondering, what is the recommended way of going about this? Inspired by ItemsControl.ItemsSource I was thinking about making the TheItems properties be of type IEnumerable, but I have trouble figuring out how to proceed.
我在想,我们该怎么做呢?灵感来自ItemsControl。ItemsSource我正在考虑将TheItems属性设置为IEnumerable类型,但是我不知道如何继续。
Any input will be appreciated!
如有任何意见,我们将不胜感激!
2 个解决方案
#1
0
in the codebehind for your display control, wich i assume is a custom control of your own creation, you can get "TheItems" and reorder them to your hearts content... since you are using an ObservableCollection, you can "attach" to the CollectionChanged event and check for the condition (see the event args being passed in) that new items are added/removed and reorder at that time.
在您的显示控件的代码后面,我认为是您自己创建的自定义控件,您可以获取“项目”并将它们重新排序到您的内心内容……因为您正在使用一个ObservableCollection,所以您可以“附加”到CollectionChanged事件中,并检查当时添加/移除新项并重新排序的条件(参见传入的事件args)。
#2
0
Dont you already have sorting implemented using MyProperty
? Why cant you use Myproperty
that can hold some calculated order that uniquely sorts the items when they are added / deleted / edited.
你没有使用MyProperty实现排序吗?为什么不能使用Myproperty,它可以保存一些计算出的顺序,以便在添加/删除/编辑条目时惟一地对它们排序呢?
I mean from the Model perspective you always know when an item is added and deleted (using ObservableCollection.CollectionChanged
handler) or edited (using INotifyPropertyChanged.PropertyChanged
handler of each item in your list). So when u handle these events, you will have to update your MyProperty
that will hold value for each item so that the reordering would take place as per your expected logic.
我的意思是,从模型的角度来看,您总是知道什么时候添加和删除一个条目(使用ObservableCollection)。集合更改处理程序)或编辑(使用INotifyPropertyChanged)。PropertyChanged列表中每个项目的处理程序)。因此,当您处理这些事件时,您将必须更新MyProperty,它将为每个项保存值,以便按照您期望的逻辑重新排序。
#1
0
in the codebehind for your display control, wich i assume is a custom control of your own creation, you can get "TheItems" and reorder them to your hearts content... since you are using an ObservableCollection, you can "attach" to the CollectionChanged event and check for the condition (see the event args being passed in) that new items are added/removed and reorder at that time.
在您的显示控件的代码后面,我认为是您自己创建的自定义控件,您可以获取“项目”并将它们重新排序到您的内心内容……因为您正在使用一个ObservableCollection,所以您可以“附加”到CollectionChanged事件中,并检查当时添加/移除新项并重新排序的条件(参见传入的事件args)。
#2
0
Dont you already have sorting implemented using MyProperty
? Why cant you use Myproperty
that can hold some calculated order that uniquely sorts the items when they are added / deleted / edited.
你没有使用MyProperty实现排序吗?为什么不能使用Myproperty,它可以保存一些计算出的顺序,以便在添加/删除/编辑条目时惟一地对它们排序呢?
I mean from the Model perspective you always know when an item is added and deleted (using ObservableCollection.CollectionChanged
handler) or edited (using INotifyPropertyChanged.PropertyChanged
handler of each item in your list). So when u handle these events, you will have to update your MyProperty
that will hold value for each item so that the reordering would take place as per your expected logic.
我的意思是,从模型的角度来看,您总是知道什么时候添加和删除一个条目(使用ObservableCollection)。集合更改处理程序)或编辑(使用INotifyPropertyChanged)。PropertyChanged列表中每个项目的处理程序)。因此,当您处理这些事件时,您将必须更新MyProperty,它将为每个项保存值,以便按照您期望的逻辑重新排序。