tags:

views:

62

answers:

2
<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?

A: 
<Application.Resources>

<Style TargetType="{x:Type Button}">
  <Setter Property="FontFamily" Value="meiryo" />
   <Setter Property="FontSize" Value="12" />
</Style>     

 <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>
  </ResourceDictionary>
</Application.Resources>

This aboved code will rise error when I try to compile it. Because I am using Japanese Visual studio version. So could you somehow understand the error message:

エラー 1 IDictionary に追加されるすべてのオブジェクトは、Key 属性またはオブジェクトに関連する別の型のキーを保持している必要があります。 行 10 位置 10. C:\Documents and Settings\huy\デスクトップ\課題\CardSystem\CardSystem\App.xaml 10 10 CardSystem

huynq9
Please don't use the answers to reply. Either use the comments area or edit your question. And sorry but no, I can't understand anything there, except a part of the path... Can't you translate that ?
Martinho Fernandes
You're lucky, I managed to get a rough translation with Google and understand it.
Martinho Fernandes
Hi Fernandes, thanks for your attention.This error means I need to add x:key to my custom <style>. Like this:<Style x:Key="Key" TargetType="{x:Type Button}">But if I do that, I have to apply this style manually whenever I want to use it. Have another ways?
huynq9
A: 

<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>
Martinho Fernandes
Thanks for your advise. But It still cannot run.BTW, I have another solutions. In AppStyle.xaml file, I create a style for window form and place my button style at there. It runs properly!
huynq9