【发布时间】:2011-02-01 14:31:48
【问题描述】:
由于某种原因,当我将 ComboBox 绑定到 POCO 列表时,绑定到 SelectedValue 的属性设置了两次:
- 值 = POCO.ToString()
- 使用 value = POCO.Key 属性,这是预期的行为
我有以下 ComboBox 绑定到我的 ViewModel 中的属性:
<ComboBox ItemsSource="{Binding Path=AllowedClassifications}"
DisplayMemberPath="Value"
SelectedValue="{Binding Path=TargetGroup.Classification}"
SelectedValuePath="Key" />
ViewModel 中的属性定义为:
//ICollection is implemented by ObservableCollection<T>
//DataBaseFieldValue has two public properties: string Key, string Value
public ICollection<DatabaseFieldValue> AllowedClassifications
{
get { return _allowedClassifications; }
private set { _allowedClassifications = value; }
}
public Model.TargetGroup TargetGroup
{
get { return _targetGroup; }
private set { _targetGroup = value; OnPropertyChanged("TargetGroup"); }
}
TargetGroup.Classification 定义为:
public string Classification
{
get { return _classification; }
set
{
System.Diagnostics.Debug.WriteLine("Classification: " + value);
_classification = value;
OnPropertyChanged("Classification");
}
}
调试输出:
分类: MyNamespace.DatabaseFieldValue
分类:2
这里发生了什么?我这样做完全错了吗?
【问题讨论】:
-
你为什么要重复你的问题??
-
可能绑定了两次? ba-da-boom-pshhh ...
-
@decyclone - 不用说这是无意的 - 我在写问题时浏览器崩溃了。我不知道它已经被创建了。
-
@johnny g - 实际上让我发笑:)
标签: wpf data-binding mvvm combobox