我试图对DataGridComboBoxColumn进行数据库化
<DataGridComboBoxColumn Header="Number of Copies" SelectedItemBinding="{Binding NumberCopies}">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding LifeAreaList}"/>
<Setter Property="IsReadOnly" Value="True"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
</DataGridComboBoxColumn>我在这里做错了什么,因为我在运行时得到了一个空的组合框。
我有跟踪
System.Windows.Data错误:2:找不到控制目标元素的FrameworkElement或FrameworkContentElement。BindingExpression:Path=LifeAreaList;DataItem=null;目标元素为“DataGridComboBoxColumn”(HashCode=49475561);目标属性为“ItemsSource”(键入“IEnumerable”)
发布于 2011-08-13 15:17:42
DataGridColumn不是从FrameworkElement或FrameworkContentElement派生的,所以它不在可视树中,doens没有DataContext,这就是绑定失败的原因。
如果您要绑定到的List<int>对于每个项都是相同的,那么也许您应该找到另一种方法来绑定到它,也许您可以使它是静态的,并在绑定中使用StaticResource。
无论如何,要将ItemsSource绑定到源类中的List<int>属性,可以使用ElementStyle和ElementEditingStyle (正如其他人所指出的)。以下几点应该有效
<DataGridComboBoxColumn Header="Number of Copies"
SelectedItemBinding="{Binding ListAreaItem}">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding LifeAreaList}"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding LifeAreaList}"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>发布于 2011-08-13 13:43:07
您不应该在样式中设置ItemsSource,列本身为有这样的财产,它可能覆盖您可能试图在样式中设置的任何内容。此外,如果尝试将其设置为错误的样式(该样式用于显示模式),则可以尝试将其设置为EditingElementStyle,但我也不建议这样做。
发布于 2011-08-13 12:21:50
为什么要将项目源设置为样式?
你能试试这段代码吗:
<my:DataGridTemplateColumn Header="Number of Copies" >
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=LifeAreaList}" >
<ComboBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding .}"></Label>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>如果DataGridTemplateColumn是复杂的类集合,并且希望以自定义的方式显示它,则为LifeAreaList定义数据模板。
https://stackoverflow.com/questions/7050286
复制相似问题