2009-08-21 75 views
0

如何在代碼(而不是XAML)中對WPF Toolkit DataGrid列標題值進行數據綁定?Databinding DataGrid列標題代碼

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

此數據綁定列的單元格的內容。但是,我如何綁定標題文本(「Foo」)本身(例如,視圖模型上的字符串屬性)?

回答

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});