以下两者的区别是什么:
设置SelectedItem SelectedValue SelectedValuePath
所有这些依赖属性都在Selector类中定义。我经常混淆SelectedItem和SelectedValue, SelectedValue和SelectedValuePath。
我想知道它们之间的区别,以及我们什么时候使用它们,特别是SelectedValue和SelectedValuePath。请用一些简单的例子解释它们的用法。
以下两者的区别是什么:
设置SelectedItem SelectedValue SelectedValuePath
所有这些依赖属性都在Selector类中定义。我经常混淆SelectedItem和SelectedValue, SelectedValue和SelectedValuePath。
我想知道它们之间的区别,以及我们什么时候使用它们,特别是SelectedValue和SelectedValuePath。请用一些简单的例子解释它们的用法。
当前回答
他们的名字可能有点令人困惑:)。以下是摘要:
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). 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).
下面的示例演示了这一点。我们有一个组合框绑定到一个类别列表(通过ItemsSource)。我们将产品上的CategoryID属性绑定为所选值(使用SelectedValue属性)。我们通过SelectedValuePath属性将其关联到Category的ID属性。我们说只在组合框中显示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; }
}
刚开始有点混乱,但希望这能让你更清楚……:)
克里斯
其他回答
他们的名字可能有点令人困惑:)。以下是摘要:
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). 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).
下面的示例演示了这一点。我们有一个组合框绑定到一个类别列表(通过ItemsSource)。我们将产品上的CategoryID属性绑定为所选值(使用SelectedValue属性)。我们通过SelectedValuePath属性将其关联到Category的ID属性。我们说只在组合框中显示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; }
}
刚开始有点混乱,但希望这能让你更清楚……:)
克里斯
更概念化地回答:
SelectedValuePath定义了绑定到ListBox的ItemsSource对象的哪个属性(通过它的名称)将被用作该项的SelectedValue。
例如,如果你的ListBox绑定到一个Person对象的集合,每个对象都有Name、Age和Gender属性,SelectedValuePath=Name将导致所选Person的Name属性的值在SelectedValue中返回。
注意,如果你重写了ListBox的ControlTemplate(或应用一个样式)来指定应该显示什么属性,SelectedValuePath不能被使用。
同时,SelectedItem返回当前选中的整个Person对象。
(下面是MSDN的另一个例子,使用TreeView)
更新:正如@Joe指出的,DisplayMemberPath属性与Selected*属性无关。正确的描述如下:
注意,这些值不同于DisplayMemberPath(它是在ItemsControl上定义的,而不是Selector),但该属性具有与SelectedValuePath类似的行为:在没有样式/模板的情况下,它标识绑定到item的对象的哪个属性应该用作它的字符串表示。
每个使用集合存储数据的控件都有SelectedValue、SelectedItem属性。这些控件的例子有ListBox, Dropdown, RadioButtonList, CheckBoxList。
更具体地说,如果你字面上想检索所选项目的文本,那么你可以这样写:
ListBox1.SelectedItem.Text;
你的ListBox1也可以返回文本使用SelectedValue属性,如果value已经设置之前。但以上是更有效的获取文本的方式。
现在,值对用户是不可见的,但它主要用于存储在数据库中。我们没有插入ListBox1的文本,但是我们也可以插入它,但是我们用来插入所选项目的值。以获得我们可以使用的价值
ListBox1.SelectedValue
源
受到这个问题的启发,我写了一篇博客,附带了这里的代码片段。以下是该博客的部分节选
SelectedItem - SelectedItem帮助绑定将显示的数据源的实际值。这是object类型,我们可以用这个属性绑定从object类型派生的任何类型。因为我们将使用MVVM绑定我们的组合框在这种情况下,这是属性,我们可以使用通知VM的项目已被选中。
SelectedValue和SelectedValuePath——这是组合框中最令人困惑和误解的两个属性。但是当我们想要将组合框与已经创建的对象的值绑定时,这些属性就会派上用场。请查看下面列表中的最后一个场景,以获得关于属性的简要概念。
SelectedItem和SelectedValue是一个对象。 SelectedValuePath是一个字符串。
例如使用ListBox:
listbox1以下。SelectedValue变成一个字符串值。
string value = listbox1.SelectedValue;
如果你说给我listbox1。SelectedItem它会给你整个对象。
ListItem item = listbox1.SelectedItem;
string value = item.value;