【问题标题】:WPF style buttonWPF 样式按钮
【发布时间】:2015-05-11 15:55:42
【问题描述】:

我已尝试更改鼠标悬停事件的样式。

这是我的样式代码:

<Window.Resources>
    <Style x:Key="NavigationButton" 
           TargetType="{x:Type Button}">
        <Setter Property="Background" Value="#295fa6"/>
        <Setter Property="BorderBrush" Value="{x:Null}"/>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Red"/>
                <Setter Property="BorderBrush" Value="{x:Null}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

我就是这样使用它的:

<Button x:Name="test"
        Grid.Column="0"" 
        Style="{StaticResource NavigationButton}">
        <Image Source="Assets/imaaa.png" />
 </Button>

并且鼠标悬停仍然具有默认颜色

【问题讨论】:

标签: c# wpf xaml styles


【解决方案1】:

您需要修改 ControlTemplate。删除 Button 上的默认行为。

<Style x:Key="NavigationButton" 
           TargetType="{x:Type Button}">
     <Setter Property="Background" Value="#295fa6"/>
        <Setter Property="BorderBrush" Value="{x:Null}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="#295fa6">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
           <Setter Property="Background" Value="Red"/>
           <Setter Property="BorderBrush" Value="{x:Null}"/>
        </Trigger>
    </Style.Triggers>
</Style>

【讨论】:

    猜你喜欢
    • 2013-07-11
    • 1970-01-01
    • 2018-11-01
    • 1970-01-01
    • 2015-02-04
    • 2011-02-10
    • 2012-06-02
    • 1970-01-01
    相关资源
    最近更新 更多