C# 从 wpf 中的单独文件加载控件样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10978079/
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-08-09 16:00:19  来源:igfitidea点击:

Load control style from separate file in wpf

c#.netwpf

提问by lebhero

I have the following style added to my Windows.Resources

我在 Windows.Resources 中添加了以下样式

<Window.Resources>
...
<!--A Style that extends the previous TextBlock Style-->
<!--This is a "named style" with an x:Key of TitleText-->
<Style BasedOn="{StaticResource {x:Type TextBlock}}"
   TargetType="TextBlock"
   x:Key="TitleText">
<Setter Property="FontSize" Value="26"/>
<Setter Property="Foreground">
 <Setter.Value>
  <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
    <LinearGradientBrush.GradientStops>
      <GradientStop Offset="0.0" Color="#90DDDD" />
      <GradientStop Offset="1.0" Color="#5BFFFF" />
    </LinearGradientBrush.GradientStops>
  </LinearGradientBrush>
  </Setter.Value>
</Setter>
</Style> 
...
</Window.Resources>

I have a lot of those styles in my xaml code and I would like to save each component style to an extra file (not an external file).. for example all the styles related to TextBlocks should be in a file called TextBlockStyles.xaml

我的 xaml 代码中有很多这些样式,我想将每个组件样式保存到一个额外的文件(而不是外部文件)中。例如,与 TextBlocks 相关的所有样式都应该在一个名为 TextBlockStyles.xaml

How would I do this in wpf?

我将如何在 wpf 中做到这一点?

How do I link the style in my project ?

如何链接项目中的样式?

Thanks in advance

提前致谢

回答by Phil

You use merged resource dictionaries

您使用合并的资源字典

In you app.xaml you would use

在您的 app.xaml 中,您将使用

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary 
                Source="/Your.Assembly.Name;component/TextBlockStyles.xaml"/>
            ... other dictionaries here
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

or directly into a UserControl would be

或直接进入 UserControl 将是

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary 
                Source="/Your.Assembly.Name;component/TextBlockStyles.xaml"/>
            ... other dictionaries here
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

You can shorten Source="..."to just Source="TextBlockStyles.xaml"if the file is in the same assembly and in the root of the project, or alternatively Source="Styles\TextBlockStyles.xaml"if you put the resource dictionary into the folder Styles.

您可以缩短Source="..."到只Source="TextBlockStyles.xaml"如果该文件是在同一个组件,并在项目的根目录,或者Source="Styles\TextBlockStyles.xaml"如果你把资源字典到该文件夹Styles

回答by JSJ

you are looking for the dynamic resources. well the best way is to load and marge the dictionary in the resources. application or either on control page. here is a good sample for it.

您正在寻找动态资源。最好的方法是在资源中加载和调整字典。应用程序或在控制页面上。这是一个很好的示例。

http://blogs.msdn.com/b/wpfsdk/archive/2007/06/08/defining-and-using-shared-resources-in-a-custom-control-library.aspx

http://blogs.msdn.com/b/wpfsdk/archive/2007/06/08/defining-and-using-shared-resources-in-a-custom-control-library.aspx

<ResourceDictionary>

  <ResourceDictionary.MergedDictionaries>

    <ResourceDictionary Source="Dictionary1.xaml"/>

  </ResourceDictionary.MergedDictionaries>

</ResourceDictionary>

this.Resources.MergedDictionaries.Add(Smyresourcedictionary);

this.Resources.MergedDictionaries.Add(Smyresourcedictionary);

回答by Ben

Use case: you have a user control called MyView.xamlwith a button. You want to style the button with an external XAML file.

用例:您有一个MyView.xaml用按钮调用的用户控件。您想使用外部 XAML 文件设置按钮样式。



In MyView.xaml:

MyView.xaml

<User Control ...namespaces...>
    <UserControl.Resources>
        <ResourceDictionary>
            ...converters...

            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="MyButton.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

     ...the rest of the control...
</UserControl>

In MyButton.xaml:

MyButton.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MSDNSample">

    <Style x:Key="FooButton" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="Pink" />
    </Style>
</ResourceDictionary>

Back to MyView.xaml("the rest of the control"):

回到MyView.xaml(“其余的控制”):

<Button Style="{StaticResource FooButton}">
    Hello World
</Button>

回答by MdRezaV

In Solution ExplorerRight Click on your ProjectSelect AddAfter that click on Resource Dictionary...Choose name and add to your project. Open App.xamlAdd This Code in ApplicationTag

解决方案资源管理器中右键单击您的项目选择添加之后单击资源字典...选择名称并添加到您的项目中。打开App.xaml应用程序标签中添加此代码

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="YourStyle.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

In YourStyle.xaml :

在 YourStyle.xaml 中:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:APPNAME">
    <Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="Pink" />
    </Style>
</ResourceDictionary>

回答by Arsen Khachaturyan

Simply, go to your Window (example: MaindWindow.xaml) where you want to include the resource from the outer file and use MergedDictionariesprinciple to refer to that file:

简单地,转到MaindWindow.xaml您想要包含外部文件中的资源的窗口(例如:)并使用MergedDictionaries原则来引用该文件:

<Window x:Class="UseMergedResource.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow"
        Height="600"
        Width="600">
  <Window.Resources>

    <!-- DECLARING MERGED DICTIONARY --> 
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source='Merged/BrushResources.xaml' />
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    <!----------------------------------> 

  </Window.Resources>


  <StackPanel>
    <Rectangle Width='200'
               Height='100'
               Fill='{StaticResource PrimaryBrush}' /> <!-- USAGE HERE -->
  </StackPanel>
</Window>

From above Merged/BrushResources.xamlis the location of the resource file, which is located under the folder called Merged.

从上面Merged/BrushResources.xaml是资源文件的位置,它位于名为Merged.

Now if you are wondering what should be the declaration syntax in the outer file, check this:

现在,如果您想知道外部文件中的声明语法应该是什么,请检查以下内容:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <!-- Location for application brushes -->
    <SolidColorBrush x:Key='BorderBrush'
                                     Color='Orange' />
    <SolidColorBrush x:Key='HighLightBrush'
                                     Color='LightBlue' />
    <SolidColorBrush x:Key='PrimaryBrush'
                                     Color='Pink' />
    <SolidColorBrush x:Key='AccentBrush'
                                     Color='Yellow' />

</ResourceDictionary>

If you want to make the resource available through all application (visible in all your windows) then declare in the App.xamlresources section.

如果您想让资源通过所有应用程序(在所有窗口中可见)可用,请在App.xaml资源部分声明。