Roxy'Pro
Roxy'Pro

Reputation: 4444

How to Bind font to my DataGridHeader from my Helper class

How to Bind font to my DataGridHeader from my Helper class, which is called Globals and it is public static, like this:

public static class Globals
{
    public static int DataGridfontSizeHeader { get; set; }
    public static int DataGridfontSizeContent { get; set; }
}

In this class I am holding values of FontSize.

Now I would like to apply values from this class to my DataGrid column header and DataGrid content (fontsize of data presented in rows and columns)

I tried something like this for header :

<Style TargetType="{x:Type DataGridColumnHeader}">
    <Setter Property="SeparatorBrush" Value="Yellow" />
    <Setter Property="Background" Value="#0091EA"/>
    <Setter Property="Opacity" Value="1"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="HorizontalContentAlignment" Value="Center" />
    <Setter Property="FontSize" Value="{Binding MyProject.Helpers.Globals.DataGridfontSizeHeader }"/>
    <Setter Property="FontFamily" Value="Arial"/>
    <Setter Property="Height" Value="40"/>
    <Setter Property="Padding" Value="0,0,17,0"/>
    <!--Margin to shift the entire header to the right 17 units to fill the void-->
    <Setter Property="Margin" Value="0,0,-17,0"/>
</Style>

But unfortunatelly it is not working,

so my question is how to apply fonts from this Global class to my DataGrid Content and to DataGrid Header columns?

Thanks guys, Cheers

Upvotes: 1

Views: 54

Answers (2)

mm8
mm8

Reputation: 169218

Change the type of the properties to double and specify a valid font size as the default value:

public static class Globals
{
    public static double DataGridfontSizeHeader { get; set; } = 10;
    public static double DataGridfontSizeContent { get; set; } = 10;
}

Then you can set the value of the property in the Style like this:

<Setter Property="FontSize" Value="{x:Static local:Globals.DataGridfontSizeHeader}"/>

"local" is a namespace mapping to the CLR namespace where your class is defined:

<Window x:Class="WpfApplication3.Window23"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication1"
    ...

Edit:

yes, this ability to have auto property initializers is included since C# 6.0 and unfortunately I'm using C# 5.0 and as you can see my class is static so I can't define it in constructor because static classes can't have a contructor, hmmmmm

Just define a static constructor:

public static class Globals
{
    static Globals()
    {
        DataGridfontSizeHeader = 10;
        DataGridfontSizeContent = 10;
    }

    public static double DataGridfontSizeHeader { get; set; }
    public static double DataGridfontSizeContent { get; set; }
}

Upvotes: 1

Arie
Arie

Reputation: 5373

First, you are trying to bind an int property of your class to a double FontSize property (it will throw an exception).

Secondly, while you can sort of "bind" to static properties (see @mm8 answer) it is a one time only sort of binding, since INotifyPropertyChanged makes sense only for instances. So the changes you'll make to your static properties on runtime wont be reflected.

The solution here would be to use a singleton pattern instead.

Upvotes: 0

Related Questions