Windows 7 中的 WPF 按钮背景

发布于 2024-09-11 23:27:31 字数 76 浏览 5 评论 0原文

我之前有 Windows XP,设置 wpf 按钮背景总是有效,但从 Windows 7 开始,背景始终设置为蓝色。我该如何解决这个问题?

I had windows xp before and setting the wpf button background always worked but since windows 7 the background is always set to blue. How could i fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风夜微凉 2024-09-18 23:27:31

您可能需要为按钮制作自己的模板。 (我想这只是您正在努力解决的悬停颜色,而不是遵循背景属性的基本颜色)。

这是一个非常简单的按钮模板,可能会帮助您入门。
BackBrush 和 ForeBrush 需要进行设置以适应。 (它们是应用程序其余部分的后部和前部,因此在本示例中它们向后看)

<Style TargetType="{x:Type Button}">
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="ClickMode" Value="Press"/>
    <Setter Property="TextBlock.TextWrapping" Value="Wrap"/>
    <Setter Property="BorderBrush" Value="#e9dbae" />
    <Setter Property="Foreground" Value="{StaticResource BackBrush}" />
    <Setter Property="Background" Value="{StaticResource ForeBrush}" />

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ButtonBase}">
                    <Border
                    x:Name="Border"
                    Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"

                        BorderThickness="2"
                    CornerRadius="2"
                        SnapsToDevicePixels="False"
                    RenderTransformOrigin="0.5,0.5"
                    TextBlock.Foreground="{TemplateBinding Foreground}" >
                        <ContentPresenter
                       Margin="2"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center"
                       />
                    </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsPressed" Value="true">
                        <Setter TargetName="Border" Property="RenderTransform">
                            <Setter.Value>
                                <ScaleTransform ScaleX="0.975" ScaleY="0.975" />
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="#999999"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

You'll probably need to make your own template for the buttons. (I guess it's just the hover colour which you're struggling with, not the basic colour, which does follow the Background property).

Here's a very simple button template, which might help you get started.
BackBrush and ForeBrush will need setting to suit. (And they're the Back and Fore of the rest of the application, so they look backwards in this example)

<Style TargetType="{x:Type Button}">
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="ClickMode" Value="Press"/>
    <Setter Property="TextBlock.TextWrapping" Value="Wrap"/>
    <Setter Property="BorderBrush" Value="#e9dbae" />
    <Setter Property="Foreground" Value="{StaticResource BackBrush}" />
    <Setter Property="Background" Value="{StaticResource ForeBrush}" />

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ButtonBase}">
                    <Border
                    x:Name="Border"
                    Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"

                        BorderThickness="2"
                    CornerRadius="2"
                        SnapsToDevicePixels="False"
                    RenderTransformOrigin="0.5,0.5"
                    TextBlock.Foreground="{TemplateBinding Foreground}" >
                        <ContentPresenter
                       Margin="2"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center"
                       />
                    </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsPressed" Value="true">
                        <Setter TargetName="Border" Property="RenderTransform">
                            <Setter.Value>
                                <ScaleTransform ScaleX="0.975" ScaleY="0.975" />
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="#999999"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
↘紸啶 2024-09-18 23:27:31

要使用背景,请将此样式添加到 Window.Resources(或任何其他应用程序资源字典):

<Style TargetType="Button">
            <Setter Property="Background" Value="Black"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Moccasin"/>
                </Trigger>
                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Background" Value="Gray"/>
                </Trigger>                
            </Style.Triggers>
        </Style>

如果您不需要重新定义按钮模板(按钮的几何外观),则此样式有效。这里我们没有指定 Style x:Key 那么该样式将应用于指定资源范围内的所有按钮。要指定具有不同外观的样式,您应该为每个样式标记 x:Key="StyleName",然后在按钮中定义样式 Style="{DynamicResource StyleName}"Style="{DynamicResource StyleName}"

For playing with Backgrounds add to Window.Resources (or any other App ResourceDictionary) this style:

<Style TargetType="Button">
            <Setter Property="Background" Value="Black"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Moccasin"/>
                </Trigger>
                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Background" Value="Gray"/>
                </Trigger>                
            </Style.Triggers>
        </Style>

This works if you don't need to redefine Button template (geometrical appearance of button). Here we aren't specifying Style x:Key then this style will be applyed to all button in specified resource scope. To specify styles with different appearances you should each style mark with x:Key="StyleName" and then in button define style Style="{DynamicResource StyleName}" or Style="{DynamicResource StyleName}"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文