DataGridComboBoxColumn数据绑定 [英] DataGridComboBoxColumn data binding

查看:273
本文介绍了DataGridComboBoxColumn数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对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>

我在这里做错了,因为我在运行时得到了一个空的组合框.

What I am doing wrong here , because I am getting an empty combobox in the run time .

我关注了

System.Windows.Data错误:2:找不到目标元素的管理FrameworkElement或FrameworkContentElement. BindingExpression:Path = LifeAreaList; DataItem = null;目标元素是"DataGridComboBoxColumn"(HashCode = 49475561);目标属性为"ItemsSource"(类型为"IEnumerable")

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=LifeAreaList; DataItem=null; target element is 'DataGridComboBoxColumn' (HashCode=49475561); target property is 'ItemsSource' (type 'IEnumerable')

推荐答案

DataGridColumn不是从FrameworkElementFrameworkContentElement派生的,因此它不在可视树中并且没有DataContext这就是您的绑定失败的原因.

DataGridColumn doesn't derive from FrameworkElement or FrameworkContentElement so it isn't in the visual tree and doens't have a DataContext and that's why your Binding is failing.

如果要绑定的List<int>对于每个项目都是相同的,那么也许您应该找到另一种绑定方法,也许可以将其设为静态并在绑定中使用StaticResource.

If the List<int> that you're binding to is the same for every item then maybe you should find another way to bind to it, maybe you could make it static and use StaticResource in the Binding.

无论如何,要将ItemsSource绑定到源类中的List<int>属性,可以使用ElementStyleElementEditingStyle(其他人指出).以下应该可以工作

Anyway, to bind ItemsSource to a List<int> property in your source class you can use ElementStyle and ElementEditingStyle (as pointed out by others). The following should work

<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>

这篇关于DataGridComboBoxColumn数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆