Databinding the DataGrid column header in code

感情迁移 提交于 2019-12-05 07:48:48

问题


How do I databind a WPF Toolkit DataGrid column header value in code (not XAML)?

DataGridColumn fooColumn =  new DataGridTextColumn 
{
  Header = "Foo",
  Binding = new Binding {Path = new PropertyPath("BindingPath"), 
                         Mode = BindingMode.OneWay}
};

This databinds the content of the cells of the column. But how do I databind the header text ("Foo") itself (to, say, a string property on a viewmodel)?


回答1:


DataGridColumn fooColumn =  new DataGridTextColumn 
{
  Binding = new Binding {Path = new PropertyPath("BindingPath"), 
                         Mode = BindingMode.OneWay}
};

BindingOperations.SetBinding(fooColumn, DataGridColumn.HeaderProperty, new Binding("Foo") { Source = yourViewModel} );



回答2:


Looks like Header is a Dependancy Property in the lastest build



来源:https://stackoverflow.com/questions/1312613/databinding-the-datagrid-column-header-in-code

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