简体   繁体   English

WPF样式,创建在XAML中跨应用程序使用的样式页面

[英]WPF styles, create style page to be used across application in XAML

I have looked across the web to see if there was a simple explanation for my problem. 我查看了网络,看看是否有一个简单的解释我的问题。 But a lot of answers are based around writing code behind (C#) which I don't think you need to do. 但是很多答案都是基于编写代码(C#),我认为你不需要这样做。

I basically want to have a style page so instead of copying and pasting the same code, I can reference that file (A bit like CSS) 我基本上想要一个样式页面,而不是复制和粘贴相同的代码,我可以引用该文件(有点像CSS)

Basically, I have a Datagrid Header with this style 基本上,我有一个这种风格的Datagrid标题

           <DataGridTextColumn.HeaderStyle>
              <Style TargetType="{x:Type DataGridColumnHeader}">
                 <Setter Property="HorizontalContentAlignment" Value="Center" />
                 <Setter Property="Foreground" Value="White"/>
                 <Setter Property="FontWeight" Value="Bold"/>
                 <Setter Property="Background" Value="LightBlue" />
              </Style>
           </DataGridTextColumn.HeaderStyle>

But at the moment I am copying and pasting this for every single DataGrid header in my app. 但目前我正在为我的应用中的每个DataGrid标头复制并粘贴此内容。 Surely there is an easy way to stop this duplication? 当然有一种简单的方法可以阻止这种重复吗?

Thanks 谢谢

Define the style under App.Resources in your App.xaml file if you want it to be applied across all DataGridColumnHeaders 如果要将其应用于所有DataGridColumnHeaders ,请在App.xaml文件中的App.Resources下定义样式

<App.Resources>
   <Style TargetType="{x:Type DataGridColumnHeader}">
      ....
   </Style>
</App.Resources>

Basically you are looking for ResourceDictionary file. 基本上你正在寻找ResourceDictionary文件。 It allows to share the same styles, templates, etc. across application. 它允许跨应用程序共享相同的样式,模板等。 To 'include' resources from ResourceDictionary in your eg. 要在您的例如中“包含”来自ResourceDictionary Window.Resources , you must add ResourceDictionary.MergedDictionaries section like this: Window.Resources ,你必须像这样添加ResourceDictionary.MergedDictionaries部分:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/MyDll;component/Styles/slCommonStyles.xaml" />
    <ResourceDictionary Source="slGridBase.xaml" />        
    <ResourceDictionary Source="../Templates/slColumnTemplates.xaml" />    
</ResourceDictionary.MergedDictionaries>

The first 'include' uses a pack uri syntax. 第一个'include'使用pack uri语法。 It's required if you are 'including' resources from another DLL library. 如果您“包含”来自另一个DLL库的资源,则需要它。

If you want this style to be applied to all of your DataGridTextColumn, add this style without x:Key in App Resources in App.xaml 如果您希望将此样式应用于所有DataGridTextColumn,请在App.xaml中添加此样式而不使用x:Key in App Resources

<App.Resources>
   <Style TargetType="{x:Type DataGridColumnHeader}">
      <Setter Property="HorizontalContentAlignment" Value="Center" />
      <Setter Property="Foreground" Value="White"/>
      <Setter Property="FontWeight" Value="Bold"/>
       <Setter Property="Background" Value="LightBlue" />
    </Style>
</App.Resources>

OR you want this on selective Column headers, define x:key on style 或者你想在选择性列标题上使用它,在样式上定义x:key

<Style x:Key="MyHeaderStyle" TargetType="{x:Type DataGridColumnHeader}">
     <Setter Property="HorizontalContentAlignment" Value="Center" />
      <Setter Property="Foreground" Value="White"/>
      <Setter Property="FontWeight" Value="Bold"/>
       <Setter Property="Background" Value="LightBlue" />
    </Style>

and use this style like <DataGridTextColumn HeaderStyle="{StaticResource MyHeaderStyle}" 并使用此样式,如<DataGridTextColumn HeaderStyle="{StaticResource MyHeaderStyle}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM