2010-11-25 77 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 2010-12-03 15:22:56

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

虽然这个工作,我将不得不这样做我的应用程序的每个窗口。我宁愿有一种窗口样式可应用于我的应用程序中的所有窗口。 – Frinavale 2010-11-25 20:12:03