20

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..

2 Answers 2

20

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>
Sign up to request clarification or add additional context in comments.

Comments

8

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}" ... />

3 Comments

+1 This is exactly what I was searching for - i.e. the use of a regular (non-static) class in a binding. Why is it that you don't need to specify "Source={... in the Binding markup?
What about If you need this instance in App.xaml code behind ? In eed to set static variable there, in OnStartup event.
@LuckyLuke82, You can use it wherever you like. It's just a class and will be initialised upon it's first use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.