如何在 XAML 中为边框设置 MouseOver 事件/触发器? [英] How to set MouseOver event/trigger for border in XAML?

查看:26
本文介绍了如何在 XAML 中为边框设置 MouseOver 事件/触发器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望当鼠标悬停在边框上时边框变为绿色,然后当鼠标不再悬停在边框上时返回蓝色.

I want the border to turn green when the mouse is over it and then to return to blue when the mouse is no longer over the border.

我尝试了这个,但没有任何运气:

I attempted this without any luck:

<Border 
    Name="ClearButtonBorder" 
    Grid.Column="1" 
    CornerRadius="0,3,3,0" 
    Background="Blue">
    <Border.Triggers>
        <Trigger Property="Border.IsMouseOver" Value="True">
            <Setter Property="Border.Background" Value="Green" />
        </Trigger>
        <Trigger Property="Border.IsMouseOver" Value="False">
            <Setter Property="Border.Background" Value="Blue" />
        </Trigger>
    </Border.Triggers>
    <TextBlock 
        HorizontalAlignment="Center" 
        VerticalAlignment="Center" 
        Text="X" />
</Border>

如何为鼠标悬停设置触发器或事件?

How can one set a trigger or events for MouseOver?

推荐答案

是的,这令人困惑...

Yes, this is confusing...

根据 这篇博文,看来这是 WPF 的一个遗漏.

According to this blog post, it looks like this is an omission from WPF.

要使其工作,您需要使用样式:

To make it work you need to use a style:

    <Border Name="ClearButtonBorder" Grid.Column="1" CornerRadius="0,3,3,0">
        <Border.Style>
            <Style>
                <Setter Property="Border.Background" Value="Blue"/>
                <Style.Triggers>
                    <Trigger Property="Border.IsMouseOver" Value="True">
                        <Setter Property="Border.Background" Value="Green" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Border.Style>
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="X" />
    </Border>

我想这个问题并不常见,因为大多数人倾向于将这类东西分解为一种样式,因此它可以用于多个控件.

I guess this problem isn't that common as most people tend to factor out this sort of thing into a style, so it can be used on multiple controls.

这篇关于如何在 XAML 中为边框设置 MouseOver 事件/触发器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆