【问题标题】:Applying default style to all wpf windows将默认样式应用于所有 wpf 窗口
【发布时间】:2017-10-11 21:50:17
【问题描述】:

我有一个 wpf 应用程序,它有 20 多个窗口,其中大部分用作对话框,我希望它们都具有相同的背景颜色。

我在资源字典中定义了 Window 的类型化样式,如下所示

<Style TargetType="{x:Type Window}">
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Window}">
                <Grid Background="{StaticResource WindowBackgroundBrush}">
                    <AdornerDecorator>
                        <ContentPresenter/>
                    </AdornerDecorator>
                    <ResizeGrip x:Name="WindowResizeGrip" HorizontalAlignment="Right" VerticalAlignment="Bottom" Visibility="Collapsed" IsTabStop="false"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="ResizeMode" Value="CanResizeWithGrip">
                        <Setter TargetName="WindowResizeGrip" Property="Visibility" Value="Visible"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我已将字典包含在应用程序和每个窗口的资源中,如下所示

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Resources/Resources.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Resources/Resources.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

在 Visual Studio 中,属性编辑器中的背景画笔显示“继承”,但值显示“白色”。我在 Visual Studio 中看到了所需的背景颜色,但是当我运行应用程序时,我仍然看到白色背景。谁能解释我在这里做错了什么? WindowBackgroundBrush 正在正确应用到其他控件。

请注意,如果我将样式简化为只是

<Style TargetType="{x:Type Window}">
    <Setter Property="Background" Value="Aqua"/>
</Style>

Visual Studio 将背景画笔显示为“样式设置器”作为值源,并将 Aqua 显示为值源,但应用启动时窗口仍为白色。

【问题讨论】:

  • @EdPlunkett 这就是&lt;Application.Resources&gt; 标签的作用。
  • @XAMlMAX 我的智商很低。对不起!
  • @EdPlunkett np。我们都有那些日子;-)
  • 我刚刚尝试重现您的问题,它也发生在我身上。我记得为窗口设置样式并不像控件那样容易。它与InitialiseComponents() 有关,但我不能指望它。
  • 谢谢。很高兴不只是我。最初的评论是否表明不需要将字典添加到 windows 资源,因为它已经添加到应用程序资源中?

标签: wpf


【解决方案1】:

你做对了。它在我的地方工作......你如何将样式应用于窗口?我申请如下..

<Window x:Class="WpfApplicationScratchpad.MainWindow"
        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:local="clr-namespace:WpfApplicationScratchpad"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525"  Style="{StaticResource Style1}">

这里 Style1 是您在资源字典中的样式。名称为“Style1”

<Style x:Key="Style1" TargetType="{x:Type Window}">
        <Setter Property="WindowStyle" Value="None"/>
        <Setter Property="AllowsTransparency" Value="True"/>
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Window}">
                    <Grid Background="Beige">
                        <AdornerDecorator>
                            <ContentPresenter/>
                        </AdornerDecorator>
                        <ResizeGrip x:Name="WindowResizeGrip" HorizontalAlignment="Right" VerticalAlignment="Bottom" Visibility="Collapsed" IsTabStop="false"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="ResizeMode" Value="CanResizeWithGrip">
                            <Setter TargetName="WindowResizeGrip" Property="Visibility" Value="Visible"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

【讨论】:

  • 好的,这对我也有用。我原以为窗口会因为它的类型而继承样式,就像我为我定义的其他控件所拥有的样式一样,但我想不会。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-28
相关资源
最近更新 更多