覆盖 WPF 中的静态资源

Avi*_*atz 5 .net wpf resources xaml staticresource

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

出于演示目的,我将资源称为“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>
Run Code Online (Sandbox Code Playgroud)

应用程序.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>
Run Code Online (Sandbox Code Playgroud)