我用一个窗口创建了简单的 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/