WPF:样式不会应用于Window子类

时间:2017-09-19 08:31:52

标签: wpf xaml

这个问题让我疯了一整天了!请帮忙!

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

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

我把事情缩小到只是试图让窗口背景变红......这根本不起作用而且我拉毛了!

PropertiesWindowBase.cs:

.headerclass {
    max-width: 1280px;
    min-width: 768px;
    background-color: #ffffff;



}

.logotest {
    margin-top: -2%;
    width: 85px;
    height: 40px;
    margin-left: 17%;
    margin-bottom: 1.5%;
}

ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #ffffff; 
    border-bottom: solid 1px;
     border-top: solid 1px;
}

li {
    float: left;
}

    li a {
        display: block;
        color: rgb(110,110,120);
        text-align: center;
        padding: 13px 50px;
        text-decoration: none;
        border-right: solid 1px;
    }

        li a:hover {
            text-decoration: none;
            color: #0099cc;

        }
.active {


    background: #0099CC;
    color: #ffffff;

}
.active a{

    color: #ffffff;
}
    .active a:hover {
        color: #ffffff;
    }
html {
    background-color: #D4D2DB;
}
body {
    margin-left: 10%;
    margin-right: 10%;
    background-color: white;
}

Styles.xml:

using System.Windows;

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

app.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.xaml.cs:

<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>

CaseUserPropertiesWindow.xaml:

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.cs:

<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>

1 个答案:

答案 0 :(得分:1)

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

您可能拥有CaseUserPropertiesWindow的样式BasedOn StyleWindowBase的样式:

<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>