Binding the Visibility property of a WPF Datagrid column? Where is my fault?

萝らか妹 提交于 2019-12-13 03:49:05

问题


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;

回答1:


You format the VisabilityList to string. You need to leave it as Visibility.




回答2:


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



来源:https://stackoverflow.com/questions/7639050/binding-the-visibility-property-of-a-wpf-datagrid-column-where-is-my-fault

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!