覆盖WPF中的静态资源

时间:2016-07-21 11:06:09

标签: .net wpf xaml resources staticresource

我想覆盖在我自己的资源字典中不同程序集的资源字典中配置的StaticResource。我尝试使用相同的密钥配置新资源,但没有成功。实际加载的资源来自上述程序集的资源字典。

出于演示目的,我将调用资源“MyResource”:

MyResourceDictionary.xaml:

<ResourceDictionary xmlns=...>
    <!-- I use the same key as the original resource, from the other assembly -->
    <DataTemplate x:Key="MyResource">
        <!--My own implementation of that resource -->
    </DataTemplate>
</ResourceDictionary>

的App.xaml

<Application x:Class=...>
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <!-- Resource dictionary from different assembly -->
                <ResourceDictionary Source="pack://application:,,,/Assembly;component/ResourceDictionary.xaml"/>
                <!-- My resource dictionary -->
                <ResourceDictionary Source="pack://application:,,,/MyApplication;component/ResourceDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

1 个答案:

答案 0 :(得分:-1)

尝试覆盖DataTemplate在WPF中不是一个好习惯。相反,模板应该被“替换”或更好地说,应该使用TemplateSelector选择。看一下这个样本: template selector microsoft