How to apply WPF style to all elements of a certain type?

<Window.Resources>   
 <Style TargetType="{x:Type Button}">    
   <Setter Property="FontFamily" Value="Times New Roman" />   
   <Setter Property="FontSize" Value="30" />    
   <Setter Property="FontWeight" Value="Bold" />     
   <Setter Property="Background" Value="#FFCA5132" /> 
 </Style>
</Window.Resources>

This labeled code looks like the right choice for applying WPF style to all buttons in a specific window. But I want to apply this style to all buttons in my program. I think I need to write this code in <Application.Resources></Application.Resources>. But that does not work. How can i do this?

+3
source share
1 answer

<Application.Resources>is ResourceDictionary. You cannot add to it both ResourceDictionary, and Stylehow you do in your code. Put Styleinside ResourceDictionary, for example:

<Application.Resources>     

 <ResourceDictionary Source="/PresentationFramework.Aero
                        , Version=3.0.0.0,Culture=neutral
                        , PublicKeyToken=31bf3856ad364e35
                        , ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml">            
    <ResourceDictionary.MergedDictionaries>
     <ResourceDictionary Source="Style\AppStyle.xaml"></ResourceDictionary>
    </ResourceDictionary.MergedDictionaries>

    <Style TargetType="{x:Type Button}">
      <Setter Property="FontFamily" Value="meiryo" />
      <Setter Property="FontSize" Value="12" />
    </Style>   
  </ResourceDictionary>
</Application.Resources>
0
source

Source: https://habr.com/ru/post/1727566/


All Articles