绑定WPF DataGrid列的可见性属性?哪里是我的错吗? [英] Binding the Visibility property of a WPF Datagrid column? Where is my fault?

查看:552
本文介绍了绑定WPF DataGrid列的可见性属性?哪里是我的错吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个循环设置我的WPF的DataGrid列,我想列知名度成员'我',我用下面的code'VisibilityList'绑定:

  VAR bindingColumnVisibilty =新的Binding(的String.Format(VisibilityList [{0}],I));
BindingOperations.SetBinding(customBoundColumn,DataGridColumn.VisibilityProperty,bindingColumnVisibilty);

我已经设置了DataContext的循环开始之前:

  TestControlDataGrid.TestDataGrid.DataContext = dataGridSource;

该dataGridSource类包含:

 公开名单<能见度和GT; VisibilityList;

此不会出现工作。已设置我的DataContext并正确绑定?不要紧,这个循环我设置的ItemsSource为以下后?

  TestDataGrid.ItemsSource = dataGridSource.DataList;


解决方案

好吧,事实证明,一个DataGridColumn不会从DataGrid继承DataContext的,因为它不是在逻辑(或视觉)举人,所以这就是为什么我绑定不工作。

一个解决方法显示<一个href=\"http://blogs.msdn.com/b/jaimer/archive/2008/11/22/forwarding-the-datagrid-s-datacontext-to-its-columns.aspx\"相对=nofollow>这里

In a loop which sets up my WPF DataGrid columns, I want to bind the column visibility to member 'i' in my 'VisibilityList' with the following code:

var bindingColumnVisibilty = new Binding(string.Format("VisibilityList[{0}]", i));
BindingOperations.SetBinding(customBoundColumn, DataGridColumn.VisibilityProperty, bindingColumnVisibilty);

I have set the DataContext before the loop begins:

TestControlDataGrid.TestDataGrid.DataContext = dataGridSource;

The dataGridSource class contains:

public List<Visibility> VisibilityList;

This does not appear to work. Have I set up my DataContext and binding correctly? Does it matter that after this loop I set the ItemsSource with the following?

TestDataGrid.ItemsSource = dataGridSource.DataList;

解决方案

Ok, it turns out that DataGridColumn does not inherit the DataContext from the DataGrid since it is not in the logical (or visual) tree, so that's why my binding doesn't work.

One workaround is shown here

这篇关于绑定WPF DataGrid列的可见性属性?哪里是我的错吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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