2013-06-28 36 views
1

我在我的project1中創建了一個主題,並引用了app.xaml中的theme.xaml。效果是所有項目在解決方案中獲得相同的主題。WPF將主題應用到特定項目而不是整個應用程序

將theme.xaml應用於指定項目(即僅適用於project1而不適用於project2)的最簡單方法是什麼?

我知道我可以使用

<Window.Resources> 
     <ResourceDictionary Source="/Project1;component/Themes/Customized.xaml" /> 
    </Window.Resources> 

PROJECT1內引用theme.xaml在每個世界糧食計劃署的形式但是,這是很難一點點來維持,如果我想改變的主題項目。我正在尋找的東西就像project.xaml,其行爲與app.xaml相似,只是範圍與當前項目相同。這樣我可以在一個地方爲指定的項目(但不是其他項目)引用theme.xaml。

這可能嗎?

在此先感謝。

回答

1
  1. 創建一個項目主題資源字典,把參考FooTheme.xaml進去。

  2. 在您項目的所有窗口中,請參考ProjectTheme.xaml

這樣,爲了改變項目的主題,你只需要修改一行。

代碼:

FooTheme.xaml(樣本主題)

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Style TargetType="Button"> 
     <Setter Property="Background" Value="Blue"/> 
    </Style> 
</ResourceDictionary> 

ProjectTheme.xaml(項目主題)

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <ResourceDictionary.MergedDictionaries> 
     <!-- In order to modify the project's theme, change this line --> 
     <ResourceDictionary Source="FooTheme.xaml"/> 
    </ResourceDictionary.MergedDictionaries> 
</ResourceDictionary> 

MainWindow.xaml(示例項目窗口)

<Window x:Class="So17372811ProjectTheme.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> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="ProjectTheme.xaml"/> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </Window.Resources> 
    <Grid> 
     <Button Content="Click me!"/> 
    </Grid> 
</Window> 
+0

優秀。十分感謝你的幫助。 – Shawn

相關問題