绑定 WPF Datagrid 列的 Visibility 属性?我的错在哪里?
在设置 WPF DataGrid 列的循环中,我想使用以下代码将列可见性绑定到“VisibilityList”中的成员“i”:
var bindingColumnVisibilty = new Binding(string.Format("VisibilityList[{0}]", i));
BindingOperations.SetBinding(customBoundColumn, DataGridColumn.VisibilityProperty, bindingColumnVisibilty);
我在循环开始之前设置了 DataContext:
TestControlDataGrid.TestDataGrid.DataContext = dataGridSource;
dataGridSource 类包含:
public List<Visibility> VisibilityList;
这确实似乎不起作用。我是否正确设置了 DataContext 和绑定?在此循环之后我使用以下内容设置 ItemsSource 是否重要?
TestDataGrid.ItemsSource = dataGridSource.DataList;
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;
您将 VisabilityList 格式化为字符串。您需要将其保留为“可见性”。
You format the VisabilityList to string. You need to leave it as Visibility.