2013-03-22 59 views
17

我想在我的應用程序中使用自定義主題,據我所知我可以通過使用資源字典並在App.xaml中引用它來實現此目的。我猜默認標籤樣式重寫用相同的價值觀,但我所有的標籤字體爲綠色如何擴展而不是覆蓋WPF樣式

<Style TargetType="{x:Type Label"> 
    <Setter Property="Foreground" Value="Green" /> 
</Style> 

現在:樣式將覆蓋默認這樣。當我想再次在某處標記一個標籤時,問題就開始了。當我想換一些其他財產在我的網格像我網格內這個

<Grid.Resources> 
    <Style TargetType="{x:Type Label"> 
     <Setter Property="FontSize" Value="28" /> 
    </Style> 
</Grid.Resources> 

所有標籤正在失去他們的前景色,再有一個默認的(我沒有覆蓋在上一步中默認?)。經過一番嘗試後,我發現要做到這一點,我必須添加另一個屬性Style聲明BasedOn={StaticResource {x:Type Label}}",它的工作原理。這對我來說很奇怪,因爲現在我必須在整個應用程序中重複使用相同的BasedOn代碼,而這不是樣式的工作原理 - 這應該是自動完成的!例如,在HTML + CSS中,樣式被繼承和合並,在WPF中它們被替換...

請注意,當我不使用任何樣式時,控件仍然會從somehwere(System Themes?)中獲得它們的外觀。我如何告訴他們在別的地方尋找默認值,所以沒有任何額外的樣式代碼,他們會認爲默認情況下他們應該是綠色的?

有什麼辦法可以自動設置BasedOn屬性?或者也許有更好的做這個總體?

+0

我已經找到答案與此http://stackoverflow.com/questions/2377055/override-overriden-wpf-theme – labm0nkey 2013-03-25 14:18:56

+0

similliar的問題,我只是重新讀一遍你的問題,如果你使用了我想知道資源字典?這聽起來像是你想要定義一種風格,只是將這種風格應用到你的xaml元素,對吧?那麼,如果你設置了一個資源字典,你可以讓你的基本風格設置所有的標籤都有一個綠色的前景,然後有另一種基於設置字體大小的風格,全部包含在代碼文件中,用作字典。然後在每個xaml元素中,將樣式分配爲'Style =「{StaticResource LargeGreen}」'或任何您稱之爲的樣式。我會試着用一個例子來編輯我的答案。 – Zack 2014-01-09 14:44:23

回答

20

我有同樣的問題。我使用了Zack的答案,並將其改進如下,所以如果您未指定樣式,則重寫的默認值仍將考慮在內。基本上你會做什麼,但只是在ResourceDictionary中做過一次。

<Window x:Class="TestWpf.RandomStuffWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Random Stuff Window"> 
    <Window.Resources> 
    <ResourceDictionary> 
     <!-- Default Label style definition --> 
     <Style TargetType="{x:Type Label}"> 
     <Setter Property="Foreground" Value="Green" /> 
     </Style> 
     <!-- Extending default style --> 
     <Style TargetType="{x:Type Label}" 
      x:Key="LargeGreenForegroundLabel" 
      BasedOn="{StaticResource {x:Type Label}}"> 
     <Setter Property="FontSize" Value="28" /> 
     </Style> 
    </ResourceDictionary> 
    </Window.Resources> 
    <StackPanel> 
    <Button Click="Button_Click">Click</Button> 
    <Label Content="GreenForegroundLabel" /> <!-- Uses default style --> 
    <Label Style="{StaticResource LargeGreenForegroundLabel}" 
      Content="LargeGreenForegroundLabel" /> 
    </StackPanel> 
</Window> 
8

Wpf具有不同的風格級別,按照global> local的順序應用。直接在控件上設置的樣式將覆蓋全局樣式集,如您的示例中所示。我試圖找到控件查找其樣式的所有不同位置的列表,但此刻我找不到一個。據我所知,你將不得不使用BasedOn屬性來繼承一個樣式,而不是用你在本地設置的樣式完全覆蓋該樣式的屬性。

下面是一個資源字典的例子,它具有基於另一種風格的樣式,因此您不必重複地重複綁定BasedOn,您可以在您想要的特定元素上設置樣式那種風​​格。

<Window x:Class="TestWpf.RandomStuffWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="Random Stuff Window"> 
    <Window.Resources> 
    <ResourceDictionary> 
     <Style TargetType="{x:Type Label}" 
      x:Key="GreenForegroundLabel"> 
     <Setter Property="Foreground" Value="Green" /> 
     </Style> 
     <Style TargetType="{x:Type Label}" 
      x:Key="LargeGreenForegroundLabel" 
      BasedOn="{StaticResource GreenForegroundLabel}"> 
     <Setter Property="FontSize" Value="28" /> 
     </Style> 
    </ResourceDictionary> 
    </Window.Resources> 
    <StackPanel> 
    <Button Click="Button_Click">Click</Button> 
    <Label Style="{StaticResource GreenForegroundLabel}" 
      Content="GreenForegroundLabel" /> 
    <Label Style="{StaticResource LargeGreenForegroundLabel}" 
      Content="LargeGreenForegroundLabel" /> 
    </StackPanel> 
</Window>