为什么风格的TargetType ="窗QUOT;从App.xaml中设置时无法正常工作? [英] Why does style TargetType="Window" not work when set from App.xaml?

查看:296
本文介绍了为什么风格的TargetType ="窗QUOT;从App.xaml中设置时无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建在VS2013一个简单的WPF项目,我想属性应用到我的主窗口。我把他们在我的的App.xaml 文件是这样的:

I'm creating a simple WPF project in VS2013 and I want to apply properties to my main Window. I set them in my App.xaml file like this:

<Application.Resources>
    <Style TargetType="Window">
        <Setter Property="Background" Value="#FF2D2D30" />
    </Style>
</Application.Resources>

的问题是,什么也不会发生。当我修改的TargetType 来格但setter属性的作品就好。为什么会这样?

The problem is that nothing happens. When I change the TargetType to Grid however, the setter property works just fine. Why does this happen?

推荐答案

有必要对窗口中添加建设

Style="{StaticResource {x:Type Window}}"

窗口在XAML:

<Window x:Class="WindowStyleHelp.MainWindow"
        Style="{StaticResource {x:Type Window}}"
        ...>

或者在这样的资源定义风格

Or define Style in resources like this:

xmlns:local="clr-namespace:MyWpfApplication"

<Application.Resources>
    <Style TargetType="{x:Type local:MainWindow}">
        <Setter Property="Background" Value="#FF2D2D30"/>
    </Style>
</Application.Resources>

这篇关于为什么风格的TargetType =&QUOT;窗QUOT;从App.xaml中设置时无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆