-1

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?

SoulB
  • 68
  • 7
  • You definitely should create a Style in XAML. It's far more convenient to create and to handle. Learning XAML is essential . – BionicCode Sep 20 '22 at 07:47
  • @BionicCode I wrote the basic of my application in XAML, but the parts I create in the code - in the OnClick methods - are C# and it would be very new to me to just throw XAML into a cs file... I just want to have everything for that Element where I create it, not split across multiple files and have to make changes here an there. – SoulB Sep 20 '22 at 08:11
  • You can still learn to understand how WPF is designed to help you to develop extensible and dynamic applications. What you are trying to achieve is usually done by using DataTemplate ([Data Templating Overview](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/data/data-templating-overview?view=netframeworkdesktop-4.8)). – BionicCode Sep 20 '22 at 10:57

1 Answers1

1

I found the solution to this exception and it was a quite simple solution.

"FontSize" takes a double as it's parameter and C# sees 18 as an "int32".

For unknown reasons is it not possible for the constructor of the "Setter" class to implicitly convert the integer to double and throws this exception.

Just adding ".0" is enough to fix everything.

var fontSizeSetter = new Setter(Control.FontSizeProperty, 18.0);

The "Setter" class seems to be very picky with the datatype it gets and if it is not correct it freaks out.

Hopefully this will help someone in the future.

SoulB
  • 68
  • 7
  • To be fair, c# does not "see" 18 as an int32. It is just that some numbers have to be declared as a specific type to avoid confussion. You could just type `18d` or a directcasting `(double)18` and it would work. Check [this answer](https://stackoverflow.com/questions/3271791/declaration-suffix-for-decimal-type) – Cleptus Sep 20 '22 at 08:06
  • @Cleptus the problem I had is, just that C# interprets numbers without a suffix, if they have no decimal sign, as an int and in that case it could just implicitly convert it to double, but decides to throw an error – SoulB Sep 20 '22 at 08:16
  • it is just that somethimes it does not do an implicit conversion. It does if you do `double myVar = 18;`, but there are other scenarios where type cannot be implicitly converted like `var myVar = 18; // myVar would be integer`. It is not being picky, c# is not vb.net (and I am glad it is not) – Cleptus Sep 20 '22 at 08:23