不可否认我是WPF的新手,但我看了看,无法找到解决这个问题的方法.
我有一个简单的对象,如:
class Item
{
....
public String Measure { get; set; }
public String[] Measures {get; }
}
Run Code Online (Sandbox Code Playgroud)
我试图绑定到具有两个文本列和组合框列的DataGrid.对于组合框列,属性Measure是当前选择并测量可能的值.
我的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>
Run Code Online (Sandbox Code Playgroud)
文本列显示得很好但组合框不显示 - 根本不显示值.绑定错误是:
System.Windows.Data错误:2:找不到目标元素的管理FrameworkElement或FrameworkContentElement.BindingExpression:路径=措施; 的DataItem = NULL; target元素是'DataGridComboBoxColumn'(HashCode = 11497055); target属性是'ItemsSource'(输入'IEnumerable')
我该如何解决?
谢谢
nh4*_*3de 11
这是最好的解决方案:
http://wpfthoughts.blogspot.com/2015/04/cannot-find-governing-frameworkelement.html
这里的想法是您将CollectionViewSource声明为静态资源,然后以声明方式将其绑定到DataGridComboBoxColumn的 ItemsSource .
创建并绑定静态CollectionViewSource:
<Page.Resources>
<CollectionViewSource x:Key="Owners" Source="{Binding Owners}"/>
</Page.Resources>
Run Code Online (Sandbox Code Playgroud)
然后绑定您的目标ItemsSource:
ItemsSource="{Binding Source={StaticResource Owners}}"
Run Code Online (Sandbox Code Playgroud)