开发者

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

开发者 https://www.devze.com 2023-02-08 04:36 出处:网络
Lets say I have a two classes like this: public class LocalResources { public Color ForegroundColor { get; set; }

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 (assumin开发者_StackOverflow社区g 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.


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

精彩评论

暂无评论...
验证码 换一张
取 消