DynamicResource ResourceKey不起作用

时间:2014-06-18 11:33:10

标签: wpf xaml dynamicresource

如何在另一个ResourceDictionary中引用_brush_backcolor001? 像这样它不起作用:

<ResourceDictionary 

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:s="clr-namespace:System;assembly=mscorlib">

    <ResourceDictionary x:Key="_rd001">
        <SolidColorBrush x:Key="_brush_backcolor001">#FF8FBC8F</SolidColorBrush>
    </ResourceDictionary>

    <Style TargetType="Paragraph" x:Key="_stylePara001">
        <Setter Property="TextElement.Background">
            <Setter.Value>
                <DynamicResource ResourceKey="_brush_backcolor001" />
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

1 个答案:

答案 0 :(得分:0)

如果要使用其他资源词典中的资源,可以通过指定ResourceDictionary.MergedDictionaries

来合并它们
<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary>
            <SolidColorBrush x:Key="_brush_backcolor001">#FF8FBC8F</SolidColorBrush>
        </ResourceDictionary>
    </ResourceDictionary.MergedDictionaries>
    <Style TargetType="Paragraph" x:Key="_stylePara001">
        <Setter Property="TextElement.Background">
            <Setter.Value>
                <DynamicResource ResourceKey="_brush_backcolor001" />
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

可以提取_brush_backcolor001以分隔RecourceDictionary文件,然后通过Source属性

引用
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="path/to/separate/resouce/file.xaml"/>
</ResourceDictionary.MergedDictionaries>