[英]WPF combobox binding and SelectedItem
I have a list containing instances of the following class: 我有一个包含以下类的实例的列表:
namespace Foo.InformationModel.Reference
{
public class ReferenceCodeTypeChar
{
public ReferenceCodeTypeChar();
public string Category { get; set; }
public string CodeValue { get; set; }
public string Description { get; set; }
public string Value { get; set; }
}
}
Here is the object that is used as the DataContext
for the window in which the combobox is and its related properties: 这是用作组合框所在的窗口及其相关属性的
DataContext
的对象:
public class MyObject
{
public List<Foo.InformationModel.Reference.ReferenceCodeTypeChar> ProgramTypes() {...}
private string _selectedProgramTypeCode;
public string SelectedProgramTypeCode
{
get
{
return _selectedProgramTypeCode;
}
set
{
if (_selectedProgramTypeCode != value)
{
_selectedProgramTypeCode = value;
OnPropertyChanged("SelectedProgramTypeCode");
}
}
}
}
Here is the xaml code behind for the combobox: 这是组合框后面的xaml代码:
<ComboBox ItemsSource="{Binding Path=ProgramTypes}"
SelectedItem="{Binding Path=SelectedProgramTypeCode, Mode=TwoWay}"
DisplayMemberPath="Description"
SelectedValuePath="Value"/>
The problem happens inside SelectedProgramTypeCode. 该问题发生在SelectedProgramTypeCode内部。 The value of the "value" variable is Foo.InformationModel.Reference.ReferenceCodeTypeChar instead of the expected string of Value property of the ReferenceCodeTypeChar object.
“值”变量的值为Foo.InformationModel.Reference.ReferenceCodeTypeChar,而不是ReferenceCodeTypeChar对象的Value属性的预期字符串。 What is wrong?
怎么了?
You have to use SelectedItem
or SelectedValuePath
in conjunction with SelectedValue
. 您必须将
SelectedItem
或 SelectedValuePath
与SelectedValue
结合使用。
See this answer Difference between SelectedItem, SelectedValue and SelectedValuePath 查看此答案SelectedItem,SelectedValue和SelectedValuePath之间的区别
You should have used SelectedValue
instead of SelectedItem
in your XAML. 您应该在XAML中使用
SelectedValue
而不是SelectedItem
。
<ComboBox
ItemsSource="{Binding Path=ProgramTypes}"
SelectedValue="{Binding Path=SelectedProgramTypeCode, Mode=TwoWay}"
DisplayMemberPath="Description"
SelectedValuePath="Value" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.