2017-09-19 9 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が最も派生型を示しています。したがって、PropertiesWindowBaseではなく、CaseUserPropertiesWindowである必要があります。 PropertiesWindowBaseのスタイルの 'BasedOn'であるCaseUserPropertiesWindowのスタイルを持つことができます。 – Clemens

+0

ありがとう@クレメンス。私は最終的にそれを理解していると思います。スタイルは、基本クラスにDefaultStyleKeyProperty.OverrideMetadataを呼び出す静的コンストラクタがない限り、基本型ではなく、最も派生した型にのみ適用されます。そして、そのスタイルはThemes \ Generic.xamlになければなりませんか?私は正しい? – user884248

+0

解決策としてこれを選択できるようにこれを回答として記述しますか?または私はすべきですか? – user884248

答えて

1

TargetTypeが最も派生型を意味します。したがって、PropertiesWindowBaseの代わりにCaseUserPropertiesWindowにする必要があります。あなたはBasedOn PropertiesWindowBaseのスタイルですCaseUserPropertiesWindowのスタイルを持っているかもしれ

<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

関連する問題