Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static binding doesn't update when resource changes

Tags:

c#

wpf

I'd first like to say I'm very new to Binding.. I've done some things in WPF already but I never used binding because concept is a bit too hard to understand for me right of the bat. Even this what I'm doing now is something i managed to salvage from a tutorial that I didn't fully understand.

In my application I have a static class with static properties and there's a static method that changes those static properties.

Example:

public static class AppStyle
{
    public static SolidColorBrush property = Brushes.Red;


    public static void ChangeTheme()
    {
        property = Brushes.Blue;
    }
}

Inside the XAML I have a control that has it's background binded to this value. I even declared the namespace properly.

...
    xmlns:style="clr-namespace:CorrectNamespace;assembly=RightAssembly"
...
<TextBox x:Name="TXT_PN" 
     Background="{Binding Source={x:Static style:AppStyle.property}}"          
     TextChanged="TXT_PN_TextChanged" 
     Text="Text"/>

When the application loads it will load the correct setting (Red color) however when things change and ChangeTheme() is called, the static class will get the new value, however the textbox's Background will not change. What am I doing wrong here? As I said, I'm very new to this and I would appreciate the solution in laymen's terms.

Thank you!

like image 570
DethoRhyne Avatar asked Jan 13 '16 09:01

DethoRhyne


1 Answers

First of all, your property is actually not a property, but a field. A minimal property declaration would look like this:

public static SolidColorBrush Property { get; set; }

Please note the name is starting with an uppercase letter, which is a widely accepted coding convention in C#.

Because you also want to have a change notification fired whenever the value of the property changes, you need to declare a property-changed event (which for non-static properties is usually done by implementing the INotifyPropertyChanged interface).

For static properties there is a new mechanism in WPF 4.5 (or 4.0?), where you can write a static property changed event and property declaration like this:

public static class AppStyle
{
    public static event PropertyChangedEventHandler StaticPropertyChanged;

    private static void OnStaticPropertyChanged(string propertyName)
    {
        StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(propertyName));
    }

    private static SolidColorBrush property = Brushes.Red; // backing field

    public static SolidColorBrush Property
    {
        get { return property; }
        set
        {
            property = value;
            OnStaticPropertyChanged("Property");
        }
    }

    public static void ChangeTheme()
    {
        Property = Brushes.Blue;
    }
}

The binding to a static property would be written with the property path in parentheses:

Background="{Binding Path=(style:AppStyle.Property)}"          
like image 145
Clemens Avatar answered Nov 19 '22 07:11

Clemens