Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding to static property in static class in WPF

I have a problem with binding values from static properties from static class.

My class:

namespace MyNamespace.Data
{
    public static class MySettings
    {
        public static Color BackgroundColor { get; set; }
        public static Color FontColor { get; set; }
    }
}

XAML:

<Page ...
       xmlns:colors="clr-namespace:MyNamespace.Data"
      ...>
 ...
<Button Grid.Column="0" Content="Text"
        Background="{Binding Source={x:Static s:MySettings.BackgroundColor}}"
        Foreground="{Binding Source={x:Static s:MySettings.FontColor}}"
        BorderBrush="{Binding Source={x:Static s:MySettings.FontColor}}"/>

and when I run this code Background is set OK but the rest remains unchanged..

like image 499
user3182398 Avatar asked Jan 10 '14 15:01

user3182398


People also ask

How do you bind a static class?

You can bind to ANY property on a static class using the x:Static markup extension but if thy do not implement any change tracking, it might cause errors on the refresh! That is, the property setter needs to raise the PropertyChanged event, just like any bound properties, to refresh properly.

Can we use static with property in C#?

In C#, static means something which cannot be instantiated. You cannot create an object of a static class and cannot access static members using an object. C# classes, variables, methods, properties, operators, events, and constructors can be defined as static using the static modifier keyword.

How does binding work in WPF?

Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data. In this mechanism, the management of data is entirely separated from the way data. Data binding allows the flow of data between UI elements and data object on user interface.


Video Answer


2 Answers

Problem is that your source properties are of a Color type and destination properties are Brush. You can create SolidColorBrush using your color like so:

<Button Content="Text">
    <Button.Background>
        <SolidColorBrush Color="{Binding Source={x:Static s:MySettings.BackgroundColor}}"/>
    </Button.Background>
    <Button.Foreground>
        <SolidColorBrush Color="{Binding Source={x:Static s:MySettings.FontColor}}"/>
    </Button.Foreground>
    <Button.BorderBrush>
        <SolidColorBrush Color="{Binding Source={x:Static s:MySettings.FontColor}}"/>
    </Button.BorderBrush>
</Button>
like image 66
dkozl Avatar answered Sep 20 '22 02:09

dkozl


You don't need to use static properties... you can declare a class using the Singleton pattern, so there can only be one instance, just like a static class. Just use normal public CLR properties in this class... something like this (but with properties):

public class StateManager
{
    private static StateManager instance;
    
    private StateManager() { }

    public static StateManager Instance
    {
        get { return instance ?? (instance = new StateManager()); }
    }

    ...
}

Then only reference it from a base view model using the Instance property like this:

public StateManager StateManager
{
    get { return StateManager.Instance; }
}

Then you can access the properties in the UI simply, like this::

<Ribbon:RibbonCheckBox Grid.Row="1" Label="Audit fields" 
    IsChecked="{Binding StateManager.AreAuditFieldsVisible}" ... />
<Ribbon:RibbonCheckBox Grid.Row="2" Label="ISRCs on results" 
    IsChecked="{Binding StateManager.AreIsrcsVisibleOnSearchResults}" ... />
like image 20
Sheridan Avatar answered Sep 18 '22 02:09

Sheridan