2011-08-03 21 views
1

我在win表單應用程序中構建了一些WPF頁面。我希望爲我的應用程序使用WPF主題。不具有App.xaml(因爲這個項目是一個雙贏的形式項目用WPF ElementHost展現WPF形式),我說我的主題資源字典在我的形式是這樣的:在WPF中定義其他資源clobbers主題

<UserControl.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="/Themes/ExpressionDark.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</UserControl.Resources> 

這適用於所有控件這張表格上加上一些其他的WPF用戶控件,但有些用戶控件的主題不起作用。我已經找到了這是這樣定義自己的資源受影響的控件:這似乎

<Grid.Resources> 
     <Style TargetType="ComboBox"> 
      <Setter Property="Margin" Value="0 2 5 2" /> 
     </Style> 
    </Grid.Resources> 

並不無論身在何處的資源都位於或什麼是資源。資源中沒有鍵控的任何東西都會阻止所有樣式目標的主題。我甚至嘗試了智能通過它在字典中查找資源:

<Style TargetType="{x:Type ComboBox}"> 
    <Setter Property="Foreground" Value="{DynamicResource TextBrush}" /> 
    <Setter Property="SnapsToDevicePixels" Value="true" /> 
    <Setter Property="Template" Value="{DynamicResource ComboBoxTemplate}" /> 
</Style> 

和修改我的風格,看起來像這樣:

<Grid.Resources> 
     <Style TargetType="ComboBox"> 
      <Setter Property="Foreground" Value="{DynamicResource TextBrush}" /> 
      <Setter Property="SnapsToDevicePixels" Value="true" /> 
      <Setter Property="Template" Value="{DynamicResource ComboBoxTemplate}" /> 
      <Setter Property="Margin" Value="0 2 5 2" /> 
     </Style> 
    </Grid.Resources> 

但是,這引起了我的組合框完全消失!

有什麼方法可以添加覆蓋目標控件的資源並且不會拋棄主題?

+0

@戴夫克萊默,爲什麼你會從更具體的'WPF的styles'的東西編輯標籤一些不太具體的'樣式'?這似乎是一個非常糟糕的主意! –

+0

我將其重新標記爲樣式,以便更多人可以找到這篇文章,因爲在wpf樣式下幾乎沒有任何東西會出現。我被告知wpf-標籤正在泄氣。無論如何,標籤描述應該反映這一點。 –

+0

有趣。這個問題是當人們選擇標籤來跟着你不能說'wpf'和'樣式',所以你可能會得到網絡,android,delphi等,但我想我不會制定規則。 –

回答

2

你只需要在StyleBasedOn屬性設置爲舊隱Style

<Grid.Resources> 
    <Style x:Key="ComboBoxStyle" 
      TargetType="{x:Type ComboBox}" 
      BasedOn="{StaticResource {x:Type ComboBox}}"> 
     <Setter Property="Margin" Value="0 2 5 2" /> 
    </Style> 

</Grid.Resources> 

編輯

所以,如果你不想使用所有的資源鍵你的組合框的,你必須更進一步。我不知道爲什麼WPF/WinForms互操作性很差地處理隱式樣式,但即使您將隱式樣式添加到資源中,並設置了BasedOn="{StaticResource ComboBoxStyle}",仍然會丟失模板。

但是,如果移動資源的用戶控件,然後你可以做適當的含蓄風格:

<UserControl.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="/Themes/ExpressionDark.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 

     <Style x:Key="ComboBoxStyle" 
       TargetType="{x:Type ComboBox}" 
       BasedOn="{StaticResource {x:Type ComboBox}}"> 
      <Setter Property="Margin" Value="0 2 5 2" /> 
     </Style> 
    </ResourceDictionary> 
</UserControl.Resources> 

<Grid> 
    <Grid.Resources> 
     <Style TargetType="{x:Type ComboBox}" 
        BasedOn="{StaticResource ComboBoxStyle}" /> 
    </Grid.Resources> 
+0

我沒有不知道你能做到這一點! :) –

+0

另一方面....它沒有工作! –

+0

它做了什麼? –