开发者

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

开发者 https://www.devze.com 2023-04-10 02:01 出处:网络
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:

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.Test开发者_StackOverflow中文版DataGrid.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;


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消