c# - 如何创建 'Private' ResourceDictionary?

标签 c# wpf xaml dictionary

<分区>

Possible Duplicate:
How to make a Style that only exists within the context of a ResourceDictionary

我正在构建一个复杂的 ResourceDictionary,它将公开我想与包含该 ResourceDictionary 的项目共享的 ControlTemplate。但是,此 ResourceDictionary 包含一系列我希望项目可用的支持样式和模板。我该怎么做?

例如:

<ResourceDictionary>
    <!-- Private dictionary items used to set up the publicly usable Omnibox templates -->
    <ResourceDictionary x:Key="PrivateDictionary">
        <Thickness x:Key="BaseValueMarginAdjustment">2,0,0,0</Thickness>            
        <!--Base Styles -->
        <Style x:Key="BaseElement" TargetType="FrameworkElement">...</Style>
        <Style x:Key="GridStyle" TargetType="Grid" BasedOn="{StaticResource BaseElement}">...</Style>
        <Style TargetType="Selector" x:Key="SelectorStyle" BasedOn="{StaticResource BaseElement}">...</Style>
        ...
    </ResourceDictionary>
    <!--Public CONTROL TEMPLATES -->
    <ControlTemplate TargetType="{x:Type local:OmniBox}"  x:Key="OBListBoxTemplate">
        <Grid x:Name="PART_Grid" Style="{StaticResource GridStyle}">
            <ListBox x:Name="PART_Value" Style="{StaticResource SelectorStyle}" />
            ...
    </ControlTemplate>
    <ControlTemplate ...>
    ...
</ResourceDictionary>

请注意,上面的方法不起作用。特别是,上面的编译,但有一个运行时错误,因为我想要公开可见的 ControlTemplates 找不到上面的私有(private)样式,如“GridStyle”。


尝试了以下方法也没有成功:

    <ResourceDictionary>
        <!-- Private dictionary items used to set up the publicly usable Omnibox templates -->
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary>...</ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>

        <!--CONTROL TEMPLATES -->
        <ControlTemplate TargetType="{x:Type local:OmniBox}"  x:Key="OBTextBoxTemplate">
        ...

最佳答案

为什么不能将这些样式资源放在单独的资源字典中?因此,在您不希望这些资源可见的项目中,不要将它们合并到那里。

假设您的 App.xaml 中有像这样定义的资源 -

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/WpfApplication1;component/Themes/Dictionary1.xaml"/>
                <ResourceDictionary Source="pack://application:,,,/WpfApplication1;component/Themes/Dictionary2.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

假设您的样式资源存在于 Dictionary2.xaml 中,只需从中省略第二个字典即可。

编辑:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/WpfApplication1;component/Themes/Dictionary1.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

您可以在 ResourceDictionary2 中添加 ResourceDictionary1 的引用。无论您希望在何处使用资源,例如任何 UserControl,您都可以随时在其中添加 ResourceDictionary1 的引用,以防您不想要全局引用。

关于c# - 如何创建 'Private' ResourceDictionary?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7767900/

相关文章:

c# - 将空值绑定(bind)到 wpf 控件的宽度和高度时出现绑定(bind)错误

c# - 如何将具有许多功能的大型 WCF 服务导入到 VS 2010 中?

c# - 将 LongListSelector 绑定(bind)到异步更新的嵌套 ObserverCollection(使用 WebClient)

c# - 在使用 MVVM 的 WPF 中,谁负责管理外部通信?

c# - 如何在 WPF ToolBar 中使用标准的 Button 样式?

c# - 确定 ListboxItem 在 Canvas 中的位置?

C# WPF - 拖动并单击

wpf - 未知的构建错误 "This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms"

并发调用的 C# 异步方法应等待第一个操作完成

c# - ServiceStack.Text json 反序列化创建错误的对象而不是抛出无效的 json 输入字符串