绑定的SelectedItem VS SelectedIndex的 - 当我应该选择一个比其他? [英] Binding SelectedItem vs SelectedIndex - When should I choose one over the other?
问题描述
让我们说,你有对象Foo类型的观察的集合,而你有一个自定义的ListView,用户将从中选择
Let's say that you have an observable collection of object type Foo, and you have a custom ListView that the user will select from.
您绑定的数据对象:
// property with getter / setter / INotifyPropertyChanged
ObservableCollection<Foo> MyCollection;
在XAML:
<ListView ItemsSource={Binding MyCollection} />
是不是更合适绑定到的SelectedIndex在XAML并创建您的数据对象中的以下内容:
Is it more appropriate to bind to the SelectedIndex in XAML and create the following in your data object:
int SelectedIndex { get; set; } // also raising property changed notifications
Foo SelectedObject
{
get { return MyCollection[SelectedIndex]; }
}
或者要创建这一点,并绑定到XAML中的SelectedItem:
Or to create this and bind to the SelectedItem in XAML:
Foo SelectedObject { get; set; } // also raising property changed notifications
和为什么?
推荐答案
两种情况是可接受的,但是其选择通常取决于数据模型的设计,并且该方法将需要的代码量最少以获得工作
Both cases are acceptable, however which you choose usually depends on the design of your data models, and which method would require the least amount of code to get working.
我用它来确定选择
-
如果其中一个少数规则
SelectedObject
不能为空(如枚举),你需要为默认将未选择任何项目,然后用的SelectedIndex
。
If
SelectedObject
cannot be null (such as an enum), and you need to default to be no item selected, then useSelectedIndex
.
如果 SelectedObject
可能不被视为 .Equals()
来的任何项目在产品
列表,使用的SelectedIndex
。这是因为的SelectedItem
比较与产品对象
使用集 .Equals()
,所以参考comparism将返回false,这将导致不becomming选择你的对象。
If SelectedObject
may not be considered .Equals()
to any item in your Items
list, use SelectedIndex
. This is because SelectedItem
compares objects with the Items
collection using .Equals()
, so a reference comparism will return false which will result in your object not becomming selected.
这时候您选择的项目来自不同的位置通常发生在您的项目清单。例如,一个数据库调用加载项目
的名单,和一个单独的数据库调用获得一个对象,它包含一个 SelectedObject
属性。
This usually happens when your selected item comes from a different location than where your list of items is. For example, one database calls to load the Items
for the list, and a separate database call obtaining an object that includes a SelectedObject
property.
如果您需要引用只有 SelectedObject
或的SelectedIndex
在你的代码的其他部分,使用一个。
If you need to reference only one of SelectedObject
or SelectedIndex
in other parts of your code, use that one.
如果您的数据模型已经有一个的SelectedIndex
或 SelectedObject
属性,然后使用它。
If your data models already have a SelectedIndex
or SelectedObject
property, then use that.
如果所有其他的事情都是平等的,我用的是 SelectedObject
属性设置为的SelectedItem
属性绑定。
If all other things are equal, I use a SelectedObject
property to bind the SelectedItem
property.
这是因为对我来说更有意义,是指像 SelectedUser
而不是 SelectedUserIndex
在后面的代码,而我更喜欢以避免在收集只要我想做的事与所选项目的东西仰视的项目。
This is because to me it makes more sense to refer to something like SelectedUser
instead of SelectedUserIndex
in the code behind, and I prefer to avoid looking up the item in the collection anytime I want to do something with the selected item.
这篇关于绑定的SelectedItem VS SelectedIndex的 - 当我应该选择一个比其他?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!