我有一个DataGrid用 XAML 编写的 WPF,我正在转换为 C#(不要问)。
它看起来像这样(为简洁起见省略了一些属性):
var Card = new DataGrid() {
Background = Brushes.LightYellow,
BorderBrush = Brushes.DimGray,
ColumnWidth = new DataGridLength(100),
Columns = {
new DataGridTextColumn() {
Binding = new Binding("In"),
Header = "In"
},
new DataGridTextColumn() {
Binding = new Binding("Out"),
Header = "Out"
},
new DataGridTextColumn() {
Binding = new Binding("Hours"),
Header = "Hours"
}
},
RowHeaderTemplate = new DataTemplate(typeof(DataGridRowHeader)) {
VisualTree = Days
},
RowHeaderWidth = 115,
RowHeight = 50
};
Days设置如下:
var Days = new FrameworkElementFactory(typeof(TextBlock));
Days.SetBinding(TextBlock.TextProperty, new Binding("Day"));
Days.SetValue(TextBlock.BackgroundProperty, Brushes.Lime);
运行时,DataGrid'sRowHeader为空白(并且LightYellow,不是Lime)。
我也试过Card.RowHeaderTemplate.VisualTree = Days;了,没用。
我哪里错了?如何以RowHeaderTemplate编程方式设置?