2010-02-04 62 views
10

我已經創建了自己的自定義轉換器,它給出一個字符串返回Brush。現在我可以返回像Brushes.Red等常量畫筆,但我真的想用我自己的顏色,這是我在應用程序範圍內定義的資源。WPF:在代碼隱藏中引用應用程序範圍內的資源

如何從我自己的自定義轉換器類引用應用程序範圍的資源?我會用FindResource,但正如我所說,這是來自我自己的轉換器類,而不是窗口或控件。

回答

2

添加到裏德的答案,如果您的資源字典是獨立的XAML文件,您需要確保它是(如裏德說的)「定義在您的應用程序」。

的App.xaml

<Application x:Class="WpfApplication10.App" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      StartupUri="MainWindow.xaml"> 
    <Application.Resources> 
     <ResourceDictionary Source="Dictionary1.xaml" /> 
    </Application.Resources> 
</Application> 

Dictionary1.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <TextBlock x:Key="k_foo" Text="FOO" /> 
</ResourceDictionary> 

這個字典XAML文件的Build Action可以設置爲Page。它應該與App.xaml文件位於同一個目錄中。