Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

styles

wpf

<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 aboved code looks like the right selection to apply WPF style to all buttons in a specific window. But I want to apply this style for all buttons in my program. I think I need to write down this code in the <Application.Resources></Application.Resources>. But it doesn't run. How can I do that?

like image 1000
huynq9 Avatar asked Nov 14 '22 13:11

huynq9


1 Answers

<Application.Resources> is a ResourceDictionary. You can't add to it both a ResourceDictionary and a Style, like you are doing in your code. Place the Style inside the ResourceDictionary, like this:

<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>
like image 152
R. Martinho Fernandes Avatar answered Dec 23 '22 06:12

R. Martinho Fernandes