1

I work on a WPF project with many UserControls and I'm trying to put some sanity in my WPF resources.

A base file contains generic stuff needed by all views, such as colors:

<!-- Resources.xaml -->
<Color x:Key="FlashOrange">#F59500</Color>

Then, a resource file specific to a view needs to reference FlashOrange:

<!-- ContactViewResources.xaml -->
<DataTrigger Binding="{Binding IsActiveConversation}" Value="True">
    <Setter Property="Background" Value="{StaticResource FlashOrange}"/>
</DataTrigger>

Finally I'd like to "include" both in my UserControl. Unfortunately, with this approach I get an exception about {StaticResource FalshOrange} not being defined.

<!-- ContactView.xaml -->
<UserControl.Resources>        
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="..\Resources.xaml"/>
            <ResourceDictionary Source="ContactViewResources.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>     
</UserControl.Resources>

All the view files are in the same DLL assembly, and a separate EXE assembly runs

Thanks!

4

2 回答 2

2

将 Resources.xaml 合并到 ContactViewResources.xaml 中,以便静态引用在加载时在其资源树中具有指向 FlashOrange 的直接路径,或者改用 DynamicResource。

于 2010-10-28T12:41:27.863 回答
1

若要在另一个中引用一个 ResourceDictionary,请使用要保存引用的 ResourceDictionary 的 MergedDictionary 属性。在你的情况下:

<!-- ContactViewResources.xaml -->
<ResourceDictionary ...>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="..\Resources.xaml"/>
    </ResourceDictionary.MergedDictionaries>  
    ...  
</ResourceDictionary>
于 2012-05-30T02:05:35.250 回答