开发者

How to reference a property from a static member in 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 (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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜