与 wpf ComboBox DisplayMemberPath、SelectedValue 和 SelectedValuePath 混淆 [英] Confused with wpf ComboBox DisplayMemberPath,SelectedValue and SelectedValuePath
问题描述
我一直在为那些组合框属性而苦恼
I have always struggled with those comboBox properties
- 显示成员路径
- 选定值
- SelectedValuePath
我正在构建一个主详细信息表单.
I am building a master detail form .
- ComboBox 装满了客户
- 用户在组合中选择客户
- 所有文本框 EG 都正确填充
我遇到的问题我已经解决了,但我不明白这些属性和差异.有没有一个解释他们做什么的例子?
The problem I am having I have made it work but I don't understand those properties and the differences. Is there a noddy example explaining what they do?
推荐答案
我想我们可以通过一个例子更好地理解这一点.看到这个类:
I think we can understand this better with an example. See this class:
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
}
以及以下 xaml:
<ComboBox ItemsSource="{Binding Source={StaticResource Employees}}"
DisplayMemberPath="Name"
SelectedValuePath="Id"/>
DisplayMemberPath
指向 Name
属性,因此 ComboBox
和 Employee
条目中显示的值包含在下拉列表中,将是 Employee
对象的 Name
属性.
DisplayMemberPath
points to the Name
property, so the value displayed in the ComboBox
and the Employee
entries contained in the drop down list, will be the Name
property of the Employee
object.
要了解另外两个,首先应该了解SelectedItem
.SelectedItem
将从 ComboBox
返回当前选中的 Employee
对象.您还可以使用 Employee
对象分配 SelectedItem
以设置 ComboBox
中的当前选择.
To understand the other two, you should first understand SelectedItem
. SelectedItem
will return the currently selected Employee
object from the ComboBox
. You can also assign SelectedItem
with an Employee
object to set the current selection in the ComboBox
.
SelectedValuePath
指向Id
,这意味着您可以通过使用<获得当前选中的Employee
的Id
代码>选定值代码>.您还可以通过将 SelectedValue
设置为 Id
(我们假设将出现在 Employees
列表中).
SelectedValuePath
points to Id
, which means you can get the Id
of currently selected Employee
by using SelectedValue
. You can also set the currently selected Employee
in the ComboBox
by setting the SelectedValue
to an Id
(which we assume will be present in the Employees
list).
这篇关于与 wpf ComboBox DisplayMemberPath、SelectedValue 和 SelectedValuePath 混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!