SelectedItem、SelectedValue 和 SelectedValuePath 之间的区别 [英] Difference between SelectedItem, SelectedValue and SelectedValuePath

查看:20
本文介绍了SelectedItem、SelectedValue 和 SelectedValuePath 之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下有什么区别:

所有这些依赖属性都在 Selector 类中定义.我经常将 SelectedItemSelectedValue 以及 SelectedValueSelectedValuePath 混淆.

All these dependency properties are defined in Selector class. I often confuse SelectedItem with SelectedValue , and SelectedValue with SelectedValuePath.

我想知道它们之间的区别,以及我们什么时候使用它们,尤其是SelectedValueSelectedValuePath.请用一些简单的例子解释它们的用途.

I would like to know the difference between them, and also when do we use them, especially SelectedValue and SelectedValuePath. Please explain their use with some simple examples.

推荐答案

他们的名字可能有点混乱 :).总结如下:

Their names can be a bit confusing :). Here's a summary:

  • SelectedItem 属性返回列表绑定到的整个对象.因此,假设您已将列表绑定到 Category 对象的集合(每个 Category 对象都具有 Name 和 ID 属性).例如.ObservableCollection.SelectedItem 属性将返回您当前选择的 Category 对象.然而,出于绑定目的,这并不总是您想要的,因为这仅使您能够将整个 Category 对象绑定到列表绑定到的属性,而不是该 Category 对象上的单个属性的值(例如它的 <代码>ID 属性).

  • The SelectedItem property returns the entire object that your list is bound to. So say you've bound a list to a collection of Category objects (with each Category object having Name and ID properties). eg. ObservableCollection<Category>. The SelectedItem property will return you the currently selected Category object. For binding purposes however, this is not always what you want, as this only enables you to bind an entire Category object to the property that the list is bound to, not the value of a single property on that Category object (such as its ID property).

因此,我们将 SelectedValuePath 属性和 SelectedValue 属性作为另一种绑定方式(您可以将它们相互结合使用).假设您有一个 Product 对象,您的视图绑定到该对象(具有 ProductName、Weight 等属性).假设您在该 Product 对象上有一个 CategoryID 属性,并且您希望用户能够从类别列表中为该产品选择一个类别.您需要将 Category 对象的 ID 属性分配给 Product 对象的 CategoryID 属性.这是 SelectedValuePathSelectedValue 属性的用武之地.您指定 Category 对象上的 ID 属性应分配给列表所在的 Product 对象上的属性绑定到使用 SelectedValuePath='ID',然后将 SelectedValue 属性绑定到 DataContext(即产品)上的属性.

Therefore we have the SelectedValuePath property and the SelectedValue property as an alternative means of binding (you use them in conjunction with one another). Let's say you have a Product object, that your view is bound to (with properties for things like ProductName, Weight, etc). Let's also say you have a CategoryID property on that Product object, and you want the user to be able to select a category for the product from a list of categories. You need the ID property of the Category object to be assigned to the CategoryID property on the Product object. This is where the SelectedValuePath and the SelectedValue properties come in. You specify that the ID property on the Category object should be assigned to the property on the Product object that the list is bound to using SelectedValuePath='ID', and then bind the SelectedValue property to the property on the DataContext (ie. the Product).

下面的示例演示了这一点.我们有一个 ComboBox 绑定到一个类别列表(通过 ItemsSource).我们将产品上的 CategoryID 属性绑定为选定值(使用 SelectedValue 属性).我们通过 SelectedValuePath 属性将此与类别的 ID 属性相关联.我们是说只在 ComboBox 中显示 Name 属性和 DisplayMemberPath 属性).

The example below demonstrates this. We have a ComboBox bound to a list of Categories (via ItemsSource). We're binding the CategoryID property on the Product as the selected value (using the SelectedValue property). We're relating this to the Category's ID property via the SelectedValuePath property. And we're saying only display the Name property in the ComboBox, with the DisplayMemberPath property).

<ComboBox ItemsSource="{Binding Categories}" 
          SelectedValue="{Binding CategoryID, Mode=TwoWay}" 
          SelectedValuePath="ID" 
          DisplayMemberPath="Name" />

public class Category
{
    public int ID { get; set; }
    public string Name { get; set; }
}

public class Product
{
    public int CategoryID { get; set; }
}

一开始有点令人困惑,但希望这能让它更清楚一点... :)

It's a little confusing initially, but hopefully this makes it a bit clearer... :)

克里斯

这篇关于SelectedItem、SelectedValue 和 SelectedValuePath 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆