我有一个简单的对象,例如:
我的XAML如下:
class Item
{
....
public String Measure { get; set; }
public String[] Measures {get; }
}
我正在尝试将其绑定到一个具有两个文本列和一个组合框列的DataGrid。
对于组合框列,属性Measure是当前选择,而Measures是可能的值。我的XAML如下:
<DataGrid Name="recipeGrid" AutoGenerateColumns="False"
CellEditEnding="recipeGrid_CellEditEnding" CanUserAddRows="False"
CanUserDeleteRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Food" Width="Auto"
Binding="{Binding Food.Name}" />
<DataGridTextColumn Header="Quantity" IsReadOnly="False"
Binding="{Binding Quantity}" />
<DataGridComboBoxColumn Header="Measure" Width="Auto"
SelectedItemBinding="{Binding Path=Measure}"
ItemsSource="{Binding Path=Measures}" />
</DataGrid.Columns>
</DataGrid>
文本列可以正常显示,但组合框无法显示值。绑定错误为:
System.Windows.Data Error: 2 : 找不到目标元素的主 FrameworkElement 或 FrameworkContentElement。BindingExpression:Path=Measures; DataItem=null; target element is 'DataGridComboBoxColumn' (HashCode=11497055); target property is 'ItemsSource' (type 'IEnumerable')
我该如何解决?
CollectionViewSource
没有在其他地方使用,您也可以直接将其放在<DataGrid.Resources>
中。 - piedar