DataGridComboBoxColumn为什么就不能在Binding的时候引用其他Named Element了呢?
2012-04-19 16:27
621 查看
下面是代码,摘自一个简单的项目,模拟的是将数据库中的内容放到表格里面,其中有一列,“资源类型”是一个ComboBox,能够让用户进行选择。资源类型绑定在2个对象中,源数据是通过Restypes数组获得,而选中项是通过ResData的FK_RESID来指定:
你可以注意到,我在窗口的Load事件中,把_TypeCombo这一列的ItemsSource通过代码绑定到了内部Restypes集合,然后在XAML中,我也设置了其ItemsSource的绑定,你可能会觉得两者重复了,但是!假如把代码去掉,整个程序就不工作了!在Debug的时候会看到会报错:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Restypes; DataItem=null; target element is 'DataGridComboBoxColumn'
(HashCode=27237168); target property is 'ItemsSource' (type 'IEnumerable')
那么是什么原因导致该绑定失效呢?究其原因,是因为Columns集合只不过是DataGrid的一个属性,不存在于逻辑树和视觉树,所以任何的绑定都是无效的,比如DataContext啊,ElementName啊,Source啊。
具体解决方法可以参考这篇文章:
http://blogs.msdn.com/b/jaimer/archive/2008/11/22/forwarding-the-datagrid-s-datacontext-to-its-columns.aspx
/// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public List<ClsResType> GameData { get; set; } public List<ResData> Restypes { get; set; } public void LoadResData() { Restypes = new List<ResData>(); Restypes.Add(new ResData() { Resid = "1", Resname = "ResName1" }); Restypes.Add(new ResData() { Resid = "2", Resname = "ResName2" }); Restypes.Add(new ResData() { Resid = "3", Resname = "ResName3" }); } public MainWindow() { GameData = new List<ClsResType>(); GameData.Add(new ClsResType() { Name = "GameData-1", FK_RESID = "1" }); GameData.Add(new ClsResType() { Name = "GameData-2", FK_RESID = "2" }); GameData.Add(new ClsResType() { Name = "GameData-3", FK_RESID = "3" }); LoadResData(); InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { _TypeCombo.ItemsSource = Restypes; } } public class ClsResType { public string Name { get; set; } public string FK_RESID { get; set; } } public class ResData { public string Resid { get; set; } public string Resname { get; set; } }
<DataGrid Name="dgrData" Height="Auto" Margin="2,2,12,2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AutoGenerateColumns="false" Background="White" ItemsSource="{Binding ElementName=ThisWindow, Path=GameData}"> <DataGrid.Columns> <DataGridTextColumn Width="80" MinWidth="20" Binding="{Binding Name}" Header="资源Name" /> <DataGridComboBoxColumn x:Name="_TypeCombo" Width="100" DisplayMemberPath="Resname" Header="资源类型" ItemsSource="{Binding ElementName=ThisWindow, Path=Restypes}" SelectedValueBinding="{Binding FK_RESID}" SelectedValuePath="Resid" /> </DataGrid.Columns> </DataGrid>
你可以注意到,我在窗口的Load事件中,把_TypeCombo这一列的ItemsSource通过代码绑定到了内部Restypes集合,然后在XAML中,我也设置了其ItemsSource的绑定,你可能会觉得两者重复了,但是!假如把代码去掉,整个程序就不工作了!在Debug的时候会看到会报错:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Restypes; DataItem=null; target element is 'DataGridComboBoxColumn'
(HashCode=27237168); target property is 'ItemsSource' (type 'IEnumerable')
那么是什么原因导致该绑定失效呢?究其原因,是因为Columns集合只不过是DataGrid的一个属性,不存在于逻辑树和视觉树,所以任何的绑定都是无效的,比如DataContext啊,ElementName啊,Source啊。
具体解决方法可以参考这篇文章:
http://blogs.msdn.com/b/jaimer/archive/2008/11/22/forwarding-the-datagrid-s-datacontext-to-its-columns.aspx
相关文章推荐
- DataGridComboBoxColumn为什么就不能在Binding的时候引用其他Named Element了呢?
- eclipse java项目中明明引入了jar包 为什么项目启动的时候不能找到jar包 项目中已经 引入了 com.branchitech.app 包 ,但时tomcat启动的时候还是报错? java.lang.ClassNotFoundException: com.branchitech.app.startup.AppStartupContextListener java.lang.ClassN
- 为什么不能建立引用的数组?
- 为什么类的定义中不能包含其自身类型,但是能包含其自身的指针或引用类型
- [VB.NET]在VB.NET中为什么引用了金山词霸的xdictgrb.dll文件还是不能实现鼠标取词呢?
- [转]在用数据绑定的时候我为什么不能把焦点移出(Tab out)我的控件?(译)
- DataGrid Binding: Custom DataGrid Column Form
- 为什么类的定义中不能包含自身类型,而可以包含其自身的指针类型和引用类型
- K3CLOUD单据上的审核人与审核日期字段怎么配置?当审核的时候,为什么不能显示是谁审核的?
- 为什么使用spinlock的时候不能睡眠
- C++的数组元素为什么不能是引用类型
- Growthpush的push通知为什么当连在wifi的时候就不能收到
- 单入口文件时候不能把引用多次的模块打印到commonChunkPlugin中
- 深入理解”引用被初始化后,不能再指向其他对象“这句话?
- Javascript验证练习。请求师兄修改bug 。出错时候为什么不能显示 “确定”按钮??
- 为什么拷贝构造函数必须为引用传递,不能是值传递?
- 格式化日期的时候出现“对象不能从 DBNull 转换为其他类型。 ”的错误
- DataGrid中DataGridComboBoxColumn
- 为什么我们不能手动转义一下用户输入的%,其他的再交给PreparedStatement转义?
- WPF使用DataGridComboBoxColumn完成绑定