Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference a property from a static member in XAML?

Tags:

c#

static

wpf

xaml

Lets say I have a two classes like this:

public class LocalResources
{
    public Color ForegroundColor { get; set; }
}

public static class OrganisationModule
{
    public static LocalResources Resources = new LocalResources 
    { 
        ForegroundColor = Color.FromRgb(32, 32, 32)
    };
}

In XAML code, why can't I do this (assuming all the right xml namespaces exist)?

<TextBlock Foreground="{x:Static Modules:OrganisationModule.Resources.ForegroundColor}" />

When I compile, I get the error: Cannot find the type 'OrganisationModule.ColorManager'. Note that type names are case sensitive.

like image 721
gerrod Avatar asked Jan 20 '23 14:01

gerrod


1 Answers

There are two mistakes here. First in the OrganisationModule class you need to provide Resources as property. Currently it is not a property, you need to write Get and/or Set

Then for Binding we need below expression

Foreground="{Binding Path=ForegroundColor,Source={x:Static Modules:OrganisationModule.Resources}}" /> 
like image 191
Jobi Joy Avatar answered Jan 30 '23 11:01

Jobi Joy