2009-11-23 57 views
7

我使用的是標準WPF主題Aero.NormalColor.xaml。它工作得很好。但對於整個應用程序,我想重寫文本框的前景色爲紅色。覆蓋App.xaml中的標準主題

我的第一次嘗試是

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary 
       Source="/PresentationFramework.Aero, Version=3.0.0.0, 
       Culture=neutral, PublicKeyToken=31bf3856ad364e35, 
       ProcessorArchitecture=MSIL;component/themes/Aero.NormalColor.xaml"> 
      </ResourceDictionary> 
     </ResourceDictionary.MergedDictionaries> 
     <Style TargetType="TextBox"> 
      <Setter Property="Foreground" Value="Red" /> 
     </Style> 
    </ResourceDictionary> 
</Application.Resources> 

嗯...文本框的所有前景色變爲紅色。但是,所有文本框都會丟失主題風格。是的,我知道我應該添加「BasedOn」。我的第二次嘗試是在樣式標籤中添加「BasedOn」。

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary 
       Source="/PresentationFramework.Aero, Version=3.0.0.0, 
       Culture=neutral, PublicKeyToken=31bf3856ad364e35, 
       ProcessorArchitecture=MSIL;component/themes/Aero.NormalColor.xaml"> 
      </ResourceDictionary> 
     </ResourceDictionary.MergedDictionaries> 
     <Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}"> 
      <Setter Property="Foreground" Value="Red" /> 
     </Style> 
    </ResourceDictionary> 
</Application.Resources> 

拋出異常。同樣WPF : Extend Theme's style - StackOverflowException

最終,我通過這個達到了我的目標。

在App.xaml中

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary 
       Source="/PresentationFramework.Aero, Version=3.0.0.0, 
       Culture=neutral, PublicKeyToken=31bf3856ad364e35, 
       ProcessorArchitecture=MSIL;component/themes/Aero.NormalColor.xaml"> 
      </ResourceDictionary> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

而且在所有窗口和用戶控制,我必須明確地設置

<UserControl.Resources> 
    <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}"> 
     <Setter Property="Foreground" Value="Red" /> 
    </Style> 
</UserControl.Resources> 

上面的代碼複製和粘貼多次,這是不容易的保持。有沒有人知道如何實現我的目標,只需將前景設置爲紅色一次

回答

2

我認爲你可以將Style添加到ResourceDictionary和合並與Aero主題是這樣的:

<Application.Resources> 
    <ResourceDictionary> 
    <ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="/PresentationFramework.Aero, Version=3.0.0.0, 
     Culture=neutral, PublicKeyToken=31bf3856ad364e35, 
     ProcessorArchitecture=MSIL;component/themes/Aero.NormalColor.xaml"> 
     </ResourceDictionary> 

     <!-- Adding the style to a resource dictionary --> 
     <ResourceDictionary> 
     <Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}"> 
      <Setter Property="Foreground" Value="Red" /> 
     </Style> 
     </ResourceDictionary> 

    </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

這應該給你的文本框的紅色前景色,而無需顯式指定每個窗戶上用戶控制。

+0

爲我工作 - 謝謝。 – djskinner 2010-01-21 09:52:09

+0

爲我工作 - 但您最好將文本框樣式放在單獨的資源字典文件(例如TextBoxStyles.xaml)中,並將添加到合併的字典中。另外,您可能會遇到合併字典的錯誤,導致樣式不適用於所創建的第一個文本框。 – Schweder 2012-03-15 13:36:02

1

我有同樣的問題,並嘗試過奧斯卡的方法。雖然,它引起了一些奇怪的行爲。特別是,這些樣式不適用於某些控件,同時適用於相同類型的其他控件。我無法找到這些控件之間的任何主要區別。

我繼續尋找解決方案,我發現一個在這裏: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/91718816-8674-4ad8-a3c8-ae283bebe224/

這還不夠完善和明確的,但它工作,至少對我來說。

簡單地說,你可以從下面的代碼得到的想法:

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary> 
       <ResourceDictionary.MergedDictionaries> 
        <ResourceDictionary Source="/PresentationFramework.Aero, Version=3.0.0.0, 
     Culture=neutral, PublicKeyToken=31bf3856ad364e35, 
     ProcessorArchitecture=MSIL;component/themes/Aero.NormalColor.xaml" /> 
       </ResourceDictionary.MergedDictionaries> 
       <Style x:Key="ExtendedTextBoxStyle" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}"> 
        <Setter Property="Foreground" Value="Red" /> 
       </Style> 
      </ResourceDictionary> 
     </ResourceDictionary.MergedDictionaries> 
     <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource ExtendedTextBoxStyle}" /> 
    </ResourceDictionary> 
</Application.Resources> 

對於可維護性和可讀性,這些嵌套的ResourceDictionary對象可以去分開XAML文件。

0

該問題的確切答案是根據當前控件的靜態資源的值設置所有自定義樣式。但是,某些控件可能不具有像ListView或ListViewItem的默認樣式。

<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}"> 
    <Setter Property="Width" Value="250" /> 
    <Setter Property="Height" Value="25" /> 
</Style> 

這種風格可以像窗口資源,電網資源,文本資源或外部資源字典任何資源字典中找到。

最後,您必須將資源字典主題添加到您的應用程序資源中,如下面的代碼將Aero主題添加到我的應用程序中。

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="/PresentationFramework.Aero, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml" /> 
      <ResourceDictionary Source="/Themes/Default.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources>