wpf 如何在WPF用户控件中合并导入和本地资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1333786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:55:03  来源:igfitidea点击:

How to combine imported and local resources in WPF user control

wpfxamlresources

提问by Tor Haugen

I'm writing several WPF user controls that need both shared and individual resources.

我正在编写几个需要共享资源和个人资源的 WPF 用户控件。

I have figured out the syntax for loading resources from a separate resource file:

我已经找到了从单独的资源文件加载资源的语法:

<UserControl.Resources>
    <ResourceDictionary Source="ViewResources.xaml" />
</UserControl.Resources>

However, when I do this, I cannot also add resources locally, like:

但是,当我这样做时,我也不能在本地添加资源,例如:

<UserControl.Resources>
    <ResourceDictionary Source="ViewResources.xaml" />
    <!-- Doesn't work: -->
    <ControlTemplate x:Key="validationTemplate">
        ...
    </ControlTemplate>
    <style x:key="textBoxWithError" TargetType="{x:Type TextBox}">
        ...
    </style>
    ...
</UserControl.Resources>

I've had a look at ResourceDictionary.MergedDictionaries, but that only lets me merge more than one external dictionary, not define further resources locally.

我看过ResourceDictionary.MergedDictionaries,但这只能让我合并多个外部字典,而不是在本地定义更多资源。

I must be missing something trivial?

我一定错过了一些微不足道的东西吗?

It should be mentioned: I'm hosting my user controls in a WinForms project, so putting shared resources in App.xaml is not really an option.

应该提到的是:我在 WinForms 项目中托管我的用户控件,因此将共享资源放在 App.xaml 中并不是一个真正的选择。

回答by Tor Haugen

I figured it out. The solution involves MergedDictionaries, but the specifics must be just right, like this:

我想到了。解决方法涉及到MergedDictionaries,但是具体的一定要恰到好处,像这样:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ViewResources.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <!-- This works: -->
        <ControlTemplate x:Key="validationTemplate">
            ...
        </ControlTemplate>
        <style x:key="textBoxWithError" TargetType="{x:Type TextBox}">
            ...
        </style>
        ...
    </ResourceDictionary>
</UserControl.Resources>

That is, the local resources must be nested withinthe ResourceDictionary tag. So the example hereis incorrect.

也就是说,本地资源必须嵌套ResourceDictionary 标签中。所以这里的例子是不正确的。

回答by Preet Sangha

Use MergedDictionaries.

使用MergedDictionaries

I got the following example from here.

我从这里得到了以下示例

File1

文件 1

<ResourceDictionary 
  xmlns=" http://schemas.microsoft.com/winfx/2006/xaml/presentation "
  xmlns:x=" http://schemas.microsoft.com/winfx/2006/xaml " > 
  <Style TargetType="{x:Type TextBlock}" x:Key="TextStyle">
    <Setter Property="FontFamily" Value="Lucida Sans" />
    <Setter Property="FontSize" Value="22" />
    <Setter Property="Foreground" Value="#58290A" />
  </Style>
</ResourceDictionary>

File 2

档案 2

   <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary Source="TextStyle.xaml" />
        </ResourceDictionary.MergedDictionaries>
      </ResourceDictionary> 

回答by Lu55

You can define local resources inside MergedDictionaries section:

您可以在 MergedDictionaries 部分中定义本地资源:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <!-- import resources from external files -->
            <ResourceDictionary Source="ViewResources.xaml" />

            <ResourceDictionary>
                <!-- put local resources here -->
                <Style x:key="textBoxWithError" TargetType="{x:Type TextBox}">
                    ...
                </Style>
                ...
            </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>