WPF DataGrid:如何使用代码执行列绑定?

时间:2013-12-20 00:43:00

标签: c# wpf xaml wpfdatagrid

这是相关专栏:

<DataGridTextColumn
    Header=" Length "
    Width="Auto"
    Binding="{Binding Path=Length, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, StringFormat=0.00;;#}"
    ElementStyle="{StaticResource RightJustified}"
    x:Name="lengthColumn">
</DataGridTextColumn>

问题是我需要在运行时将该列绑定到其他属性(称为Length48)。

TIA。

1 个答案:

答案 0 :(得分:3)

试试这个:

DataGridTextColumn dataGridTextColumn = new DataGridTextColumn();
dataGridTextColumn.Header = " Length ";
dataGridTextColumn.Binding = new Binding("Length48");

YourDataGrid.Columns.Add(dataGridTextColumn);