WPF窗口的背景颜色不是由样式自动设置的

标签 wpf xaml

我用一个窗口创建了简单的 WPF 应用程序。我想要的是自动将背景颜色应用于所有窗口。但是,没有应用颜色。
这里是 link示例项目。以下是 App 中的 XAML:

<Application x:Class="SampleWPFApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:SampleWPFApp"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <Style TargetType="Window">
            <Setter Property="Background">
                <Setter.Value>
                    <SolidColorBrush Color="#FF3B444B" />
                </Setter.Value>
            </Setter>
        </Style>
        <Style TargetType="local:MainWindow" BasedOn="{StaticResource Window}" />
    </Application.Resources>
</Application>

逻辑取自 here .

编辑:

好吧,我对什么时候没有设置背景并不感兴趣——它没有在 VS 编辑器中设置。程序运行时,后台正常。 Ragavan 和 mm8 的解决方案确实有效,尽管它们与 BasedOn="{StaticResource Window}" 的区别相同。让我们省略样式的键(Window 键本身)。

唉,编辑器不显示背景,尽管显式设置样式(在 MainWindow 的 XAML 中)会使背景出现。

最佳答案

基于 不会直接绑定(bind) window 。替换此代码 BasedOn="{StaticResource {x:Type Window}}"
应用程序.Xaml

<Application x:Class="SampleWPFApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:SampleWPFApp"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <Style TargetType="Window">
            <Setter Property="Background">
                <Setter.Value>
                    <SolidColorBrush Color="#FF3B444B" />
                </Setter.Value>
            </Setter>
        </Style>
        <Style TargetType="local:MainWindow" BasedOn="{StaticResource {x:Type Window}}"/>
    </Application.Resources>
</Application>

关于WPF窗口的背景颜色不是由样式自动设置的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42384844/

相关文章:

c# - 在 wpf 中移动无边框窗口

c# - 使用按钮单击事件清除/删除文本框内容

c# - 如何使 TreeView 和 ListBox 的 SelectedItem 保持同步?

C# WPF Mysql 组合框重复值

c# - 坏主意还是好主意? WPF : Apply styles in code-behind programmatically

wpf - 如何在 Visual Studio Express 2010 中创建安装程序 (msi)?

c# - 如果我单击它上面的按钮,WPF 子窗口将关闭

c# - WPF 命令不适用于 MVVM 应用程序中的子菜单项

c# - 未找到列表框 DisplayMemberPath 属性

c# - 简单的数据注释验证未显示在 View 中(无红色网格线)