简体   繁体   English

如何绑定WPF Datagrid列的width属性?

[英]How do I bind the width property of a WPF Datagrid column?

In a loop which sets up my WPF DataGrid columns, I want to bind the column width to member 'i' in my 'WidthList' with the following code: 在设置我的WPF DataGrid列的循环中,我想使用以下代码将列宽度绑定到我的'WidthList'中的成员'i':

var bindingColumnWidth = new Binding(string.Format("WidthList[{0}]", i));
customBoundColumn.Width = bindingColumnWidth;

However, this gives me the error: 但是,这给了我错误:

Cannot implicitly convert type 'System.Windows.Data.Binding' to 'System.Windows.Controls.DataGridLength' 无法将类型'System.Windows.Data.Binding'隐式转换为'System.Windows.Controls.DataGridLength'

How can I resolve this? 我该如何解决这个问题?

DataGridColumn没有SetBinding方法,你应该试试这个:

BindingOperations.SetBinding(customBoundColumn, DataGridColumn.WidthProperty, bindingColumnWidth);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM