2011-02-02 35 views
4

可以说,我有两个类是这样的:如何从XAML中的静态成员引用属性?

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.

回答

9

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

那么对于绑定,我们当然需要下面表达

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

啊,!实际上,我不需要让Resource成为一个属性,因为它是静态的,但除此之外,这绝对是我需要的答案。谢谢! – gerrod 2011-02-02 02:26:38