2010-11-25 5 views
17

私はアプリケーションで使用されるコントロールのスタイル定義を含むResourceDictionaryを持っています。WPFウィンドウスタイルが適用されていません

すべてのスタイルがウィンドウのコントロールに正しく適用されますが、ウィンドウ自体のResourceDictionaryのスタイルは適用されません。

これは私が私の窓に適用するスタイルが含まれていることに私のResourceDictionaryでXAMLです:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:primatives="clr-namespace:System.Windows.Controls.Primitives;assembly=PresentationFramework" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Style TargetType="{x:Type Window}"> 
     <Setter Property="Background" Value="#FF121212"></Setter> 
     <Setter Property="Height" Value="768"></Setter> 
     <Setter Property="Width" Value="1024"></Setter> 
    </Style> 
<!-- .... --> 
</ResourceDictionary> 

これは私が(にこのスタイルを取得しようとして働いているウィンドウのXAMLです)適用:

<Window x:Class="TryingStyles" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="TryingStyles"> 
    <Window.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="Resources/StylesDictionary.xaml" /> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </Window.Resources>  
    <StackPanel> 
     <StackPanel Orientation="Horizontal"> 
      <Label Content="Label" Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="Label1" VerticalAlignment="Top" /> 
      <TextBox Height="23" HorizontalAlignment="Left" Margin="56,14,0,0" Name="TextBox1" VerticalAlignment="Top" Width="120" /> 
     </StackPanel> 
     <StackPanel Orientation="Horizontal"> 
      <TabControl Height="206" HorizontalAlignment="Left" Margin="12,43,0,0" Name="TabControl1" VerticalAlignment="Top" Width="250"> 
       <TabItem Header="TabItem1" Name="TabItem1"> 
        <Grid></Grid> 
       </TabItem> 
      </TabControl> 
      <GroupBox Header="GroupBox1" Margin="268,43,12,12" Width="396"></GroupBox> 
     </StackPanel> 
    </StackPanel> 
</Window> 

私がIDEの「デザインビュー」ウィンドウを表示したときが、私はスタイルが適用されていないアプリケーションを実行すると、ウィンドウのスタイルが適用されることが表示されます。

誰かが間違っていることを知っていますか?

答えて

24

あなたの問題に対する適切な解決策が存在しないことが表示されます。スタイル内のTargetTypeは派生型を管理しません。 あなたのスタイルにキーを置いて、そのスタイルをすべてのWindowsに適用することができます。

<!-- Resource file -->  
    <ResourceDictionary ...> 
     <Style TargetType="{x:Type Window}" x:Key="WindowDefaultStyle"> 
      <!-- .... -->  
     </Style> 
    </ResourceDictionary> 

    <!-- Window file --> 
    <Window Style="{DynamicResource ResourceKey=WindowDefaultStyle}"> 

また、スタイルのBasedOnプロパティを使用することもできます。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:my="clr-namespace:WpfApplication1"> 
    <Style TargetType="{x:Type Window}" x:Key="BaseStyle"> 
     <Setter Property="Background" Value="#FF121212"></Setter> 
     <Setter Property="Height" Value="768"></Setter> 
     <Setter Property="Width" Value="1024"></Setter> 
    </Style> 

    <!-- Inherit from the BaseStyle and define for the MainWindow class --> 
    <Style TargetType="{x:Type my:MainWindow}" BasedOn="{StaticResource ResourceKey=BaseStyle}" /> 
</ResourceDictionary> 
+0

ご協力いただきありがとうございますNicolas :) – Frinavale

+0

また、このソリューションをチェックする必要があります:http://stackoverflow.com/questions/431940/how-to-set-default-wpf-window-style-in-app-xaml –

3

デザイナーとはうまく動作しますが、アプリケーションを実行すると非常に奇妙です。 問題はあなたのスタイルのTargetTypeのようです。 WpfはWindowクラスと派生クラスTryingStylesを一致させることができないようです。

あなたのTargetTypeを変更

、それが動作します:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:primatives="clr-namespace:System.Windows.Controls.Primitives;assembly=PresentationFramework" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:my="clr-namespace:WpfApplication1"> 
    <Style TargetType="{x:Type my:TryingStyles}"> 
     <Setter Property="Background" Value="#FF121212"></Setter> 
     <Setter Property="Height" Value="768"></Setter> 
     <Setter Property="Width" Value="1024"></Setter> 
    </Style> 
    <!-- .... --> 
</ResourceDictionary> 
+1

これは機能しますが、アプリケーションのすべてのウィンドウでこれを行う必要があります。むしろ私のアプリのすべてのウィンドウに適用できる1つのウィンドウスタイルを持っています。 – Frinavale

関連する問題