WPF - 仅清除某些资源字典

时间:2009-10-11 08:28:14

标签: wpf resources themes

我通过清除所有合并的词典(Resources.MergedDictionaries.Clear())和基于所选主题的新词典,将“主题”应用于我的WPF应用程序。  
我想清除某些与主题相关的词典,而不是清除所有词典。我怎样才能做到这一点?在迭代它们时,我没有找到区分词典的方法......

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我假设您正在Application级别进行合并,否则您可以引入一个中间控件,其唯一的工作是托管主题词典。在这种情况下,我建议使用多层次的方法,其中第一个合并的字典包含所有与主题相关的词典:

<Application.Resources>
    <!-- all application level resources -->
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <!-- theme-related resources -->
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <!-- merge in theme-related dictionaries here -->
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>

            <!-- merge in other application-level dictionaries here -->
        </ResourceDictionary.MergedDictionaries>

        <!-- other resources -->
        <SolidColorBrush x:Key="Foo">Black</SolidColorBrush>
    </ResourceDictionary>
</Application.Resources>

现在,您只能使用以下代码定位与主题相关的资源:

Application.Current.Resources.MergedDictionaries[0].Clear();
Application.Current.Resources.MergedDictionaries[0].Add(...);