I want to create a "DataGrid" in "WPF" when a "Button" is pressed.
Everything went fine, until I wanted to change the "FontSize" of the "ColumnHeader".
My Code so far:
var list = new ObservableCollection<TestClassForDataGrid>
{
new() { id = 1, name = "Herbert", bday = DateTime.Now },
new() { id = 2, name = "Harald", bday = DateTime.Now }
};
var fontSizeSetter = new Setter(Control.FontSizeProperty, 18);
var columnHeaderStyle = new Style(typeof(DataGridColumnHeader))
{
Setters = { fontSizeSetter }
};
var dataGrid = new DataGrid
{
ItemsSource = list,
AutoGenerateColumns = false,
ColumnHeaderStyle = columnHeaderStyle
};
dataGrid.Columns.Add(new DataGridTextColumn()
{ Binding = new Binding(path: "id"), Header = "ID", FontSize = 18 });
dataGrid.Columns.Add(new DataGridTextColumn()
{ Binding = new Binding(path: "name"), Header = "Name", FontSize = 18 });
dataGrid.Columns.Add(new DataGridTextColumn()
{ Binding = new Binding(path: "bday"), Header = "Birthday", FontSize = 18 });
Grid.SetColumn(dataGrid, 1);
Grid.SetRow(dataGrid, 1);
Grid.SetColumnSpan(dataGrid, 3);
Grid.SetRowSpan(dataGrid, int.MaxValue);
GridContent.Children.Add(dataGrid);
That is the Code inside my "OnClick" method.
Whenever I try to call this Method, I get an "Exception" saying, the Value 18 is not a valid property for "FontSize".
I also tried to set it as a string, but that gave mt the same error.
The "FontSize" in the "DataGridTextcolumn" works fine.
Why do I get this error?