2017-09-19 84 views
1

这个问题一直让我疯狂一整天!请帮忙!WPF:风格不适用于Window子类

在一个空白的测试应用程序中,我创建了一个System.Windows.Window的子类,并在资源字典中应用了一个样式,并且它工作正常。

现在我在我的实际应用程序中也做了同样的事情,并且所有内容都可以构建并运行 - 但样式不适用于窗口!

我把事情缩小到只是想让窗口背景变成红色......这根本行不通,而且我把头发拉出来!

PropertiesWindowBase.cs:

using System.Windows; 

namespace MyApp.Client.UI.Windows 
{ 
    public class PropertiesWindowBase : Window 
    { 
    } 
} 

Styles.xml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:windows="clr-namespace:MyApp.Client.UI.Windows"> 
<Style TargetType="{x:Type windows:PropertiesWindowBase}"> 
    <Setter Property="Background" Value="Red"/> 
</Style> 

app.xml中:

<Application x:Class="MyApp.Client.App" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Startup="App_OnStartup"> 
<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Resources/Styles.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

App.xaml.cs:

using System.Globalization; 
using System.Threading; 
using System.Windows; 

namespace MyApp.Client 
{ 
    /// <summary> 
    /// Interaction logic for App.xaml 
    /// </summary> 
    public partial class App : Application 
    { 
     private void App_OnStartup(object sender, StartupEventArgs e) 
     { 
      var window = new CaseUserPropertiesWindow();// { DataContext = vm }; 
      window.ShowDialog(); 
      return; 
     } 
    } 
} 

CaseUserPropertiesWindow.xaml:

<windows:PropertiesWindowBase x:Class="MyApp.Client.UI.Windows.CaseUserPropertiesWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:windows="clr-namespace:MyApp.Client.UI.Windows" 
    mc:Ignorable="d" 
    Title="CaseUserPropertiesWindow" Height="300" Width="300"> 
</windows:PropertiesWindowBase> 

CaseUserPropertiesWindow.xaml.cs:

{ 
    /// <summary> 
    /// Interaction logic for CaseUserPropertiesWindow.xaml 
    /// </summary> 
    public partial class CaseUserPropertiesWindow : PropertiesWindowBase 
    { 
     public CaseUserPropertiesWindow() 
     { 
      InitializeComponent(); 
     } 
    } 
} 
+2

的TargetType表示最派生类型。因此它应该是CaseUserPropertiesWindow而不是PropertiesWindowBase。你可能有一个CaseUserPropertiesWindow的样式,它是PropertiesWindowBase的样式'BasedOn'。 – Clemens

+0

谢谢@Clemens。我想我现在终于明白了:所以样式只会应用于最派生类型,而不应用于基类型 - 除非基类具有调用DefaultStyleKeyProperty.OverrideMetadata的静态构造函数?然后样式必须在Themes \ Generic.xaml中?我对吗? – user884248

+0

你想写这个作为答案,以便我可以选择这个解决方案吗?还是应该? – user884248

回答

1

TargetType表示最派生类型。因此它应该是CaseUserPropertiesWindow而不是PropertiesWindowBase

你可能有一个CaseUserPropertiesWindow风格是BasedOn为PropertiesWindowBase的风格:

<ResourceDictionary ...> 
    <Style TargetType="windows:PropertiesWindowBase"> 
     <Setter Property="Background" Value="Red"/> 
    </Style> 
</ResourceDictionary> 

... 

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

    <Style TargetType="local:CaseUserPropertiesWindow" 
      BasedOn="{StaticResource {x:Type windows:PropertiesWindowBase}}"> 
    </ResourceDictionary> 
</Application.Resources> 
+0

谢谢。这正是我所期待的。 – user884248