【发布时间】:2014-09-03 14:46:35
【问题描述】:
我的 ViewModel 中有一个 DataGridComboBoxColumn 绑定到 ObservableCollection<String>。此绑定有效,提供 ObservableCollection 的下拉列表:
这是我的 Xaml:
<DataGrid Margin="5" AutoGenerateColumns="False" ItemsSource="{Binding MembershipsCollection}">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Region" Width="SizeToHeader">
<DataGridComboBoxColumn.ElementStyle>
<Style>
<Setter Property="ComboBox.ItemsSource" Value="{Binding DataContext.RegionShortCodeCollection, RelativeSource={RelativeSource AncestorType={x:Type vm:AccountsViewModel}}}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style>
<Setter Property="ComboBox.ItemsSource" Value="{Binding DataContext.RegionShortCodeCollection, RelativeSource={RelativeSource AncestorType={x:Type v:AccountsView}}}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
<DataGridTextColumn Header="ProductCode" Binding="{Binding Path=ProductCode}" Width="SizeToHeader"/>
</DataGrid.Columns>
</DataGrid>
& 我的 ViewModel 中的 ObservableCollection。这是由构造函数调用的方法填充的:
private ObservableCollection<String> _RegionShortCodeCollection;
public ObservableCollection<String> RegionShortCodeCollection
{
get { return _RegionShortCodeCollection; }
set
{
_RegionShortCodeCollection = value;
OnPropertyChanged("RegionShortCodeCollection");
}
}
我现在想将单元格的值绑定到数据库中的实际当前值(与图片中的 PRODUCTCODE 相同的ObservableCollection<T>,因为目前这显然是空白的:
有没有办法绑定到同一列的多个属性?如果没有,有没有办法解决这个问题?希望清楚,谢谢。
【问题讨论】:
-
DataGrid 的 ItemsSource 是什么?通常,您会将 ComboBox 的 SelectedItem 绑定到保存此值的 ItemsSource 的属性。
-
更新了问题 Xaml。如果我尝试绑定到 ItemSource 集合中的项目,我会收到“附加信息:在 AddNew 或 EditItem 事务期间不允许‘DeferRefresh’”异常...
-
这是什么?
RelativeSource={RelativeSource AncestorType={x:Type vm:AccountsViewModel}}为什么编辑样式会绑定到不同的 AncestorType? -
另外,DisplayMemberPath 绑定对我来说似乎很奇怪。您不需要设置此属性,因为您将 ItemsSource 绑定到字符串集合......没有路径。即使你正在绑定它......它不应该是一个集合,而是一个字符串。
-
谢谢@LeeO。 Xaml 已被编辑。仍然没有运气。尝试绑定到此集合项目时,在 AddNew 或 EditItem 事务异常期间不允许“延迟刷新”。我可以绑定到另一个 ObservableCollection
没问题...