更改鼠标悬停时的样式

发布于 2024-12-08 22:59:21 字数 93 浏览 1 评论 0原文

我有两种样式的按钮。第一个用于正常状态,第二个用于 mouseOver 状态。如何改变鼠标进入时的按钮样式?在 Blend 中,我尝试创建故事板并更改样式,但什么也没发生。

I have 2 styles for a button. The first one is for normal state and the second is for mouseOver state. How can I change the button style when the mouse enters it? In Blend I tried to create a storyboard and change the style but nothing happened.

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

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

发布评论

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

评论(1

抽个烟儿 2024-12-15 22:59:21

您可以将 Style 属性绑定到 IsMouseOver 并使用通用的“真值”/“假值”转换器。

您可以像这样指定转换器

<Window.Resources>
    <Style TargetType="Button" x:Key="normalStyle">
        <Setter Property="Foreground" Value="Green"/>
    </Style>
    <Style TargetType="Button" x:Key="mouseOverStyle">
        <Setter Property="Foreground" Value="Red"/>
    </Style>
    <converters:BooleanObjectConverter FalseValue="{StaticResource normalStyle}"
                                       TrueValue="{StaticResource mouseOverStyle}"
                                       x:Key="styleConverter"/>
</Window.Resources>

,然后将 Style 绑定到 IsMouseOver

<Button Style="{Binding RelativeSource={RelativeSource Self},
                        Path=IsMouseOver,
                        Converter={StaticResource styleConverter}}" 
        ... />

BooleanObjectConverter

public class BooleanObjectConverter : IValueConverter
{
    public object TrueValue { get; set; }
    public object FalseValue { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if ((bool)value == true)
        {
            return TrueValue;
        }
        return FalseValue;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

You could bind the Style property to IsMouseOver and use a generic "true value"/"false value" converter.

You can specify the converter like this

<Window.Resources>
    <Style TargetType="Button" x:Key="normalStyle">
        <Setter Property="Foreground" Value="Green"/>
    </Style>
    <Style TargetType="Button" x:Key="mouseOverStyle">
        <Setter Property="Foreground" Value="Red"/>
    </Style>
    <converters:BooleanObjectConverter FalseValue="{StaticResource normalStyle}"
                                       TrueValue="{StaticResource mouseOverStyle}"
                                       x:Key="styleConverter"/>
</Window.Resources>

And then bind Style to IsMouseOver

<Button Style="{Binding RelativeSource={RelativeSource Self},
                        Path=IsMouseOver,
                        Converter={StaticResource styleConverter}}" 
        ... />

BooleanObjectConverter

public class BooleanObjectConverter : IValueConverter
{
    public object TrueValue { get; set; }
    public object FalseValue { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if ((bool)value == true)
        {
            return TrueValue;
        }
        return FalseValue;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文