简体   繁体   English

我如何从孩子那里获得父母收藏的计数?

[英]How do I get Count of parent collection from child?

I've got an ObservableCollection assigned to ItemsSource of a listbox. 我有一个ObservableCollection分配给一个列表框的ItemsSource。 Listbox is using a DataTemplate which has a usercontrol in it which has items bound to each listboxitem's properties. 列表框使用的数据模板中有一个用户控件,该控件具有绑定到每个列表框属性的项目。

I have an up and down button on the usercontrol which moves an item up or down the list. 我在用户控件上有一个向上和向下按钮,可将项目上移或下移列表。 The list is sorted by the property that I'm changing. 该列表按我要更改的属性排序。 Click up or down, the DisplayOrder property is changed, I'm using INotifyProperty to tell the ObservableCollection it needs to re-sort. 单击上或下,DisplayOrder属性被更改,我使用INotifyProperty告诉ObservableCollection它需要重新排序。

What is the best way for the usercontrol to get the item count so that I can disable the down button when an item reaches the bottom of the list. 用户控件获取项目计数的最佳方法是什么,以便当项目到达列表底部时可以禁用向下按钮。 (The top is easy, I compare to 0) (最简单的是,我比较为0)

listBox1.Items.Count ? listBox1.Items.Count? this.Parent.Controls.Count? this.Parent.Controls.Count?

I see two ways of handling this. 我看到两种处理方式。

The first is to pass a handle of your collection to each of your items (when they get added to the collection) so that they can calculate if they are the first or last item themselves. 首先是将集合的句柄传递给每个项目(当它们添加到集合中时),以便它们可以计算它们本身是第一个项目还是最后一个项目。

The other is to expose writable properties on your items, such as CanGoUp and CanGoDown , and your parent control becomes responsible for setting these properties properly. 另一种是在项目上公开可写属性,例如CanGoUpCanGoDown ,并且父控件将负责正确设置这些属性。 I prefer this solution because it decouples the behavior of your parent list, from the child items. 我更喜欢这种解决方案,因为它可以将父列表的行为与子项分离。 Even though the up/down buttons are placed on your child items, it's really a functionality of the parent list. 即使向上/向下按钮位于子项上,它实际上也是父级列表的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM