2011-02-02 37 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