【问题标题】:Why doesn't style-setting of the window background work?为什么窗口背景的样式设置不起作用?
【发布时间】:2011-10-17 15:27:30
【问题描述】:

这是 App.xaml:

<Application>
<Application.Resources>
    <ResourceDictionary>
        <Style TargetType="Window">
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
        </Style>
    </ResourceDictionary>
</Application.Resources>
</Application>

我也有 MainWindow.xaml。在 VS 的设计模式下查看时,它的背景确实是灰色的,应该是灰色的。无论如何,当应用程序运行时,窗口的背景是默认的白色。

为什么?

如何解决这个问题?我希望所有窗口默认都具有标准背景。

【问题讨论】:

    标签: wpf xaml app.xaml


    【解决方案1】:

    问题是在运行时,您的窗口类型将是MainWindow,而不是Window。隐式样式不适用于 TargetType 的派生类型。所以你的样式不会被应用。

    在设计期间,您正在设计您的 MainWindow,但我怀疑它会创建一个 Window 作为基础。

    您需要更改类型以匹配您的窗口类型。

    【讨论】:

    • 谢谢!它在设计视图中工作的事实令人困惑。
    【解决方案2】:

    根据CodeNaked 的回答,您必须为您拥有的每个Window 创建一个Style,但您可以使用与BasedOn 这样的所有BasedOn 相同的样式

    <Application.Resources>
        <ResourceDictionary>
            <Style TargetType="Window">
                <Setter Property="SnapsToDevicePixels" Value="True"/>
                <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
            </Style>
            <Style TargetType="{x:Type local:MainWindow}"
                   BasedOn="{StaticResource {x:Type Window}}"/>
            <Style TargetType="{x:Type local:SomeOtherWindow}"
                   BasedOn="{StaticResource {x:Type Window}}"/>
            <!-- Add more Windows here... -->
        </ResourceDictionary>
    </Application.Resources
    

    【讨论】:

    • 谢谢!甚至 也有效。
    猜你喜欢
    • 2014-09-08
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多