2011-12-13 48 views
0

我想一個主題應用到了一堆WPF項目。有一個程序集包含generic.xaml和不同的應用程序。據我所知,我不能使用ResourceDictionaryLocation.ExternalLocation的ThemeInfo屬性,因爲名稱必須與我的程序相同,但我有不止一個程序...應用主題WPF和使用風格

因此,我搜索,發現我只必須包含字典作爲MergedDictionary在app.xaml

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="/ClassLibrary1;component/Themes/generic.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

這基本上工作。但是,如果使用一個樣式的控件將不再適用generic.xaml風格:在ClassLibrary1.dll

generic.xaml

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

<Style TargetType="{x:Type Button}"> 
    <Setter Property="Background" 
      Value="Black" /> 
</Style> 

窗口程序

<Window x:Class="Theming.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 

    <Window.Resources> 
     <Style TargetType="Button" 
       x:Key="ButtonGreenTextStyle"> 
      <Setter Property="Foreground" 
        Value="Green" /> 
     </Style> 
    </Window.Resources> 

    <Grid> 
     <Button Style="{DynamicResource ButtonGreenTextStyle}" Content="Test" /> 
    </Grid> 
</Window> 

我所要做的,是將WPF瞭解我的generic.xaml的風格basestyle所有按鈕(我知道我還必須寫一個控件模板,上面的代碼只是爲了SIMPLIC兩者均)

回答

0

兩件事情我會嘗試

  • 創建一個通用的ButtonBase風格/模板來設置所有 按鈕
  • 嘗試使用上ButtonGreeTextStyle一個BasedOn屬性, 使其基於現有的外觀樣式。
+0

據我所知,WPF不會爲基類的應用模板。 BasedOn會工作,但我想知道如果WPF不應用樣式默認,因爲它在generic.xaml中定義 – SACO