【问题标题】:WPF Button Trigger Style Not recognizing backgroundWPF按钮触发样式不识别背景
【发布时间】:2014-10-23 05:56:26
【问题描述】:

我正在研究按钮样式,悬停效果给我带来了一些问题。我进行了搜索,但没有找到我的答案(或者更可能没有正确搜索)所以我决定在这里问。我在我的 WPF 用户控件中指定以下样式,然后在按钮上设置此样式。

<Style x:Key="TileButton" TargetType="Button">
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="BorderBrush" Value="Black" />
            <Setter Property="Height" Value="80" />
            <Setter Property="Width" Value="80" />
            <Setter Property="Margin" Value="10,10,0,0" />
            <Setter Property="Background" Value="#FF91b220" />
            <Setter Property="FontWeight" Value="Bold" />
            <Setter Property="Foreground" Value="White" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="#FF91b220" />
                </Trigger>
            </Style.Triggers>

        </Style>

似乎一切正常,除了当我将鼠标悬停在按钮上时,而不是背景颜色如我所愿保持一致,它恢复为默认的 WPF 浅蓝色颜色。我尝试添加其他属性更改,例如增加鼠标悬停时的高度,只是为了确认这些更改生效。好像只是背景想屏蔽我。

TIA

【问题讨论】:

    标签: c# wpf wpf-controls wpf-style


    【解决方案1】:

    您必须完全覆盖继承的 Windows 按钮样式。尝试将 followinf sn-p 添加到您的样式中。

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="{TemplateBinding Background}">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    

    同时移动你的设置器(或者更确切地说是它们的内容)作为模板内边框的边框。

    【讨论】:

    • 谢谢!这很有帮助。
    【解决方案2】:

    按钮的这种行为是在默认按钮样式中定义的,“鼠标悬停”颜色被定义为绑定。要在鼠标悬停时保留颜色,您可以覆盖按钮模板,例如:

    <Style  TargetType="Button">
                    <Setter Property="OverridesDefaultStyle" Value="True"/>
                    <Setter Property="FontWeight" Value="Bold" />
                    <Setter Property="Foreground" Value="White" />                   
    
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="Button">
                                <Border BorderThickness="1"
                                        Margin="10,10,0,0"                         
                                        BorderBrush="Black"                            
                                        Background="#FF91b220"
                                        Height="80"
                                        Width="80">
    
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
    
    
                </Style>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-02
      • 2012-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-17
      • 2023-03-06
      相关资源
      最近更新 更多