2011-01-10 65 views
3

我正在處理的應用程序有2個ResourceDictionary,DefaultStyles.xaml和CustomStyles.xaml。使用BasedOn屬性和在不同字典中定義的樣式

CustomStyles字典中的樣式是否可能使用其他字典中定義的基礎樣式?

DefaultStyles.xaml:

<Style x:Key="TextBlockDefaultStyle" TargetType="TextBlock"> 
    <Setter Property="Margin" Value="4" /> 
</Style> 

CustomStyles.xaml:

<Style x:Key="SectionTitleStyle" TargetType="TextBlock" BasedOn="{StaticResource TextBlockDefaultStyle}"> 
    <Setter Property="FontSize" Value="16" /> 
</Style> 

App.xaml中:

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Assets/Styles/DefaultStyles.xaml"/> 
      <ResourceDictionary Source="Assets/Styles/CustomStyles.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

當C Ode運行時拋出以下異常:

找不到名稱/關鍵字TextBlockDefaultStyle的資源。

如果兩種樣式都在同一個文件中,它會很好地工作。

回答

6

您需要直接引用其他風格的字典。

CustomStyles.xaml:

<ResourceDictionary.MergedDictionaries> 
    <ResourceDictionary Source="DefaultStyles.xaml" /> 
</ResourceDictionary.MergedDictionaries> 

<Style x:Key="SectionTitleStyle" TargetType="TextBlock" BasedOn="{StaticResource TextBlockDefaultStyle}"> 
    <Setter Property="FontSize" Value="16" /> 
</Style> 
+2

此行爲是reasonable.But我只是有一個問題:支持算法FMP = 「{StaticResource的{X:類型按鈕}}」 沒有工作,直到我把它改成通過鍵而不是類型來引用。這看起來像WPF運行時錯誤。 – alehro 2017-06-23 10:15:51

相關問題