2010-02-04 49 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文件位于同一个目录中。

相关问题