【问题标题】:How to reference a property from a static member in XAML?如何从 XAML 中的静态成员引用属性?
【发布时间】:2011-02-02 00:55:56
【问题描述】:

假设我有两个这样的类:

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

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

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

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

编译时出现错误:Cannot find the type 'OrganisationModule.ColorManager'. Note that type names are case sensitive.

【问题讨论】:

    标签: c# wpf xaml static


    【解决方案1】:

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

    那么对于绑定我们需要下面的表达式

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

    【讨论】:

    • 啊,当然!实际上,我不需要将 Resource 设为属性,因为它是静态的,否则这绝对是我需要的答案。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 2014-08-21
    • 2014-02-17
    • 2014-07-14
    • 1970-01-01
    • 1970-01-01
    • 2018-05-31
    相关资源
    最近更新 更多