2017-09-19 79 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