2011-01-10 1 views
3

私が取り組むアプリケーションは、2つのResourceDictionary、DefaultStyles.xamlおよびCustomStyles.xamlを持っています。別の辞書で定義されたStyleで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です:BASEDON = "{StaticResource {X:Typeボタン}}" 私はそれを変更するまで働いていませんでした型の代わりにキーで参照します。これはWPFランタイムのバグのようです。 – alehro

+1

@ alehroさんのコメントでは、私はウィンドウ内のパネルのリソースに '