【发布时间】:2014-09-03 12:12:37
【问题描述】:
我正在尝试在 WPF 数据网格中包含一个 ComboBox 列。我正在尝试将此列绑定到 ViewModel 中的可观察集合,但是,在运行时单元格保持为空。数据上下文是正确的,因为所有普通列都成功绑定。我想在 UI 中显示“regionShortCode”。这是我的 xaml:
<DataGridComboBoxColumn Header="Region" DisplayMemberPath="regionShortCode" Width="SizeToHeader">
<DataGridComboBoxColumn.ElementStyle>
<Style>
<Setter Property="ComboBox.ItemsSource" Value="{Binding Path=MembershipsCollection}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style>
<Setter Property="ComboBox.ItemsSource" Value="{Binding Path=MembershipsCollection}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
& 这是我在 ViewModel 中声明的 ObservableCollection。集合已从构造函数中调用的方法成功填充:
private ObservableCollection<tbAccountMembership> _MembershipsCollection;
public ObservableCollection<tbAccountMembership> MembershipsCollection
{
get { return _MembershipsCollection; }
set
{
_MembershipsCollection = value;
OnPropertyChanged("MembershipsCollection");
}
}
在运行时我得到:
System.Windows.Data Error: 40 : BindingExpression path error: 'MembershipsCollection' property not found on 'object' ''tbAccountMembership_041E43AFC29975F12C156BA1373ACD47FC07BBE55614E5AF8AD3BBD9F090C133' (HashCode=46247020)'. BindingExpression:Path=MembershipsCollection; DataItem='tbAccountMembership_041E43AFC29975F12C156BA1373ACD47FC07BBE55614E5AF8AD3BBD9F090C133' (HashCode=46247020); target element is 'TextBlockComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')
在“输出”窗口中。为什么 Xaml 无法解析此绑定?谢谢
【问题讨论】:
-
我正在尝试将此列绑定到 ViewModel 中的可观察集合...对吗?您想将数据绑定到视图模型中的单个集合属性,而不是数据项类中的一个吗?如果是这样,那么您对 Why can't the Xaml resolve this binding? 的回答是因为您没有告诉框架在哪里查找实际属性...您需要使用
RelativeSource Binding. -
我想绑定到 ObservableCollection 中的单个元素
-
ObservableCollection<T>声明在哪里? -
在 ViewModel 中作为公共属性。问题已更新。