wpf SelectedItem、SelectedValue 和 SelectedValuePath 之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4902039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:33:40  来源:igfitidea点击:

Difference between SelectedItem, SelectedValue and SelectedValuePath

wpfsilverlightxamlselecteditemselectedvalue

提问by Nawaz

What is the difference betweeen the following:

以下有什么区别:

All these dependency properties are defined in Selectorclass. I often confuse SelectedItemwith SelectedValue, and SelectedValuewith SelectedValuePath.

所有这些依赖属性都在Selector类中定义。我经常混淆SelectedItemSelectedValue,并SelectedValueSelectedValuePath

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

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

回答by Chris Anderson

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

他们的名字可能有点令人困惑:)。这是一个总结:

  • The SelectedItemproperty returns the entire object that your list is bound to. So say you've bound a list to a collection of Categoryobjects (with each Category object having Name and ID properties). eg. ObservableCollection<Category>. The SelectedItemproperty will return you the currently selected Categoryobject. 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 IDproperty).

  • Therefore we have the SelectedValuePathproperty and the SelectedValueproperty as an alternative means of binding (you use them in conjunction with one another). Let's say you have a Productobject, that your view is bound to (with properties for things like ProductName, Weight, etc). Let's also say you have a CategoryIDproperty 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 CategoryIDproperty on the Product object. This is where the SelectedValuePathand the SelectedValueproperties 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 SelectedValueproperty to the property on the DataContext (ie. the Product).

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

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

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)。我们将产品上的 CategoryID 属性绑定为选定值(使用 SelectedValue 属性)。我们通过 SelectedValuePath 属性将此与类别的 ID 属性相关联。我们是说只在 ComboBox 中显示 Name 属性和 DisplayMemberPath 属性)。

<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... :)

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

Chris

克里斯

回答by Dan J

To answer a little more conceptually:

从概念上回答一点:

SelectedValuePathdefines which property (by its name) of the objects bound to the ListBox's ItemsSourcewill be used as the item's SelectedValue.

SelectedValuePath定义绑定到 ListBox 的对象的哪个属性(通过其名称)ItemsSource将用作项目的SelectedValue.

For example, if your ListBox is bound to a collection of Personobjects, each of which has Name, Age, and Genderproperties, SelectedValuePath=Namewill cause the value of the selected Person's Nameproperty to be returned in SelectedValue.

例如,如果你的列表框被绑定到的集合Person的对象,其每一个具有的NameAgeGender属性,SelectedValuePath=Name将导致所选的值PersonName属性中被返回SelectedValue

Note that if you override the ListBox's ControlTemplate (or apply a Style) that specifies what property should display, SelectedValuePathcannot be used.

请注意,如果您覆盖指定应显示什么属性的 ListBox 的 ControlTemplate(或应用样式),SelectedValuePath则不能使用。

SelectedItem, meanwhile, returns the entire Personobject currently selected.

SelectedItem,同时返回Person当前选中的整个对象。

(Here's a further example from MSDN, using TreeView)

(这是MSDN 的另一个示例,使用 TreeView)

Update:As @Joe pointed out, the DisplayMemberPath property is unrelated to the Selected* properties. Its proper description follows:

更新:正如@Joe 指出的, DisplayMemberPath 属性与 Selected* 属性无关。它的正确描述如下:

Note that these values are distinct from DisplayMemberPath(which is defined on ItemsControl, not Selector), but that property has similar behavior to SelectedValuePath: in the absence of a style/template, it identifies which property of the object bound to item should be used as its string representation.

请注意,这些值不同于DisplayMemberPath(在 ItemsControl 上定义,而不是在 Selector 上定义),但该属性具有与以下相似的行为SelectedValuePath:在没有样式/模板的情况下,它标识绑定到 item 的对象的哪个属性应用作其字符串表示。

回答by capdragon

SelectedItemis an object. SelectedValueand SelectedValuePathare strings.

SelectedItem是一个objectSelectedValue并且SelectedValuePathstrings。

for example using the ListBox:

例如使用列表框:

if you say give me listbox1.SelectedValueit will return the text of the currently selected item.

如果您说给我listbox1.SelectedValue,它将返回当前所选项目的文本。

string value = listbox1.SelectedValue;

if you say give me listbox1.SelectedItemit will give you the entire object.

如果你说给我,listbox1.SelectedItem它会给你整个对象。

ListItem item = listbox1.SelectedItem;
string value = item.value;

回答by Vikram

inspired by this question I have written a blog along with the code snippet here. Below are some of the excerpts from the blog

受这个问题的启发,我写了一篇博客以及此处代码片段。以下是部分博客摘录

SelectedItem – Selected Item helps to bind the actual value from the DataSource which will be displayed. This is of type object and we can bind any type derived from object type with this property. Since we will be using the MVVM binding for our combo boxes in that case this is the property which we can use to notify VM that item has been selected.

SelectedItem – Selected Item 有助于绑定来自将显示的数据源的实际值。这是对象类型,我们可以使用此属性绑定从对象类型派生的任何类型。由于我们将在这种情况下为组合框使用 MVVM 绑定,因此我们可以使用该属性通知 VM 该项目已被选中。

SelectedValue and SelectedValuePath – These are the two most confusing and misinterpreted properties for combobox. But these properties come to rescue when we want to bind our combobox with the value from already created object. Please check my last scenario in the following list to get a brief idea about the properties.

SelectedValue 和 SelectedValuePath – 这是组合框最容易混淆和误解的两个属性。但是当我们想要将组合框与已经创建的对象的值绑定时,这些属性就派上用场了。请检查我在以下列表中的最后一个场景,以简要了解这些属性。

回答by kamalpreet

Every control that uses Collections to store data have SelectedValue, SelectedItem property. Examples of these controls are ListBox, Dropdown, RadioButtonList, CheckBoxList.

每个使用集合存储数据的控件都有 SelectedValue、SelectedItem 属性。这些控件的示例是 ListBox、Dropdown、RadioButtonList、CheckBoxList。

To be more specific if you literally want to retrieve Text of Selected Item then you can write:

更具体地说,如果您真的想检索所选项目的文本,那么您可以编写:

ListBox1.SelectedItem.Text;

Your ListBox1 can also return Text using SelectedValue property if value has set to that before. But above is more effective way to get text.

如果值之前已设置为该值,则您的 ListBox1 还可以使用 SelectedValue 属性返回文本。但以上是获取文本的更有效方法。

Now, the value is something that is not visible to user but it is used mostly to store in database. We don't insert Text of ListBox1, however we can insert it also, but we used to insert value of selected item. To get value we can use

现在,该值对用户不可见,但主要用于存储在数据库中。我们不插入ListBox1 的Text,但是我们也可以插入它,但是我们曾经插入选定项的值。为了获得价值,我们可以使用

ListBox1.SelectedValue

Source

来源