0

触发器不工作。IsMouseOver 不会改变按钮的样式。我不确定我的代码有什么问题。我正在尝试更改鼠标悬停属性上按钮的样式属性。它不断抛出错误。

基本上我试图在鼠标悬停事件上使用不同的样式

 <Style x:Key="FancyButtonStyle" TargetType="{x:Type Button}">
                    <Setter Property="FontSize" Value="{Binding FontSize}"/>
                    <Setter Property="Foreground" Value="{Binding Foreground}"/>
                    <Setter Property="FontWeight" Value="{Binding FontWeight}"/>
                    <Setter Property="Width" Value="{Binding Width}"/>
                    <Setter Property="Height" Value="{Binding Height}"/>
                    <Setter Property="Background">
                        <Setter.Value>
                            <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                <GradientStop Offset="0.0" Color="White" />
                                <GradientStop Offset="0.2" Color="DarkGray" />
                                <GradientStop Offset="0.6" Color="Black" />
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Style" Value="{StaticResource ResourceKey=FancyButtonInvertStyle}"/>
                         </Trigger>
                    </Style.Triggers>
                </Style>

                <Style x:Key="FancyButtonInvertStyle" TargetType="{x:Type Button}">
                    <Setter Property="FontSize" Value="{Binding FontSize}"/>
                    <Setter Property="Foreground" Value="{Binding Foreground}"/>
                    <Setter Property="FontWeight" Value="{Binding FontWeight}"/>
                    <Setter Property="Width" Value="{Binding Width}"/>
                    <Setter Property="Height" Value="{Binding Height}"/>
                    <Setter Property="Background">
                        <Setter.Value>
                            <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                <GradientStop Offset="0.0" Color="Black" />
                                <GradientStop Offset="0.4" Color="DarkGray" />
                                <GradientStop Offset="0.6" Color="White" />
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                </Style>
4

1 回答 1

0

与其尝试更改Style中的Trigger,不如Background直接更改其中的 。让你也不会Style完全重复一遍。

<Style x:Key="FancyButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="FontSize" Value="{Binding FontSize}"/>
    <Setter Property="Foreground" Value="{Binding Foreground}"/>
    <Setter Property="FontWeight" Value="{Binding FontWeight}"/>
    <Setter Property="Width" Value="{Binding Width}"/>
    <Setter Property="Height" Value="{Binding Height}"/>
    <Setter Property="Background">
        <Setter.Value>
            <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                <GradientStop Offset="0.0" Color="White" />
                <GradientStop Offset="0.2" Color="DarkGray" />
                <GradientStop Offset="0.6" Color="Black" />
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                    <GradientStop Offset="0.0" Color="Black" />
                    <GradientStop Offset="0.4" Color="DarkGray" />
                    <GradientStop Offset="0.6" Color="White" />
                </LinearGradientBrush>
            </Setter.Value>
            </Setter>
         </Trigger>
    </Style.Triggers>
</Style>

我不确定您是否甚至可以按照您想要的方式更改Style自身Trigger,因为它不是通过Setter目标控件本身中的标记设置的。

于 2015-07-06T18:03:48.800 回答