如何在XAML中引用静态成员的属性?

时间:2021-08-10 16:57:13

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)?

在XAML代码中,为什么我不能这样做(假设存在所有正确的xml命名空间)?

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

当我编译时,我收到错误:找不到类型'OrganisationModule.ColorManager'。请注意,类型名称区分大小写。

1 个解决方案

#1


9  

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

这里有两个错误。首先在OrganisationModule类中,您需要提供Resources作为属性。目前它不是属性,您需要编写Get和/或Set

Then for Binding we need below expression

然后对于Binding,我们需要以下表达式

Foreground="{Binding Path=ForegroundColor,Source={x:Static Modules:OrganisationModule.Resources}}" /> 

#1


9  

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

这里有两个错误。首先在OrganisationModule类中,您需要提供Resources作为属性。目前它不是属性,您需要编写Get和/或Set

Then for Binding we need below expression

然后对于Binding,我们需要以下表达式

Foreground="{Binding Path=ForegroundColor,Source={x:Static Modules:OrganisationModule.Resources}}" />