WPF-即使鼠标不在上方,IsMouseOver仍为true [英] WPF - IsMouseOver Still True Even If Mouse Not Over

查看:59
本文介绍了WPF-即使鼠标不在上方,IsMouseOver仍为true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题,为什么它是True,而不应该是True.我在这里谈论XAML,并控制属性 IsMouseOver .可以通过创建这样的触发器(样式示例)来进行测试:

Simple question, why is it True while it should NOT be. I'm talking XAML here, and controls property IsMouseOver. This can be tested by creating a trigger like this (style example):

<Style x:Key="ButtonStyle" TargetType="Button">
    <Setter Property="OverridesDefaultStyle" Value="True"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}">
                    <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="#FFE5E5E5"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="Background" Value="#FFCACACB"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

如果在按住控件(按钮)的同时按下鼠标主键(左键-右手),则控件的背景颜色为 IsMouseOver 状态,直到释放按钮为止(默认状态))或返回按钮( IsPressed 状态).

If you leave the primary mouse button (Left Button - Right Handed) pressed while leaving the control (button), the control will have background color of IsMouseOver state until the button is released (Default state) or got back over the button (IsPressed state).

为什么会发生?有什么解决方法吗?

Why is it happening ? Is there any workaround ?

修改
终于找到了原因.似乎它使用MouseLeave事件将属性设置为False,并且在释放鼠标按钮之前不会触发MouseLeave事件(这就是 SledgeHammer 使用MouseMove事件的原因).如果您在表单/窗口上只有一个按钮(任何控件),则可以使用接受的答案中的代码,但是如果您像我一样拥有独立的控件(用户/自定义控件),则无法使用它(如果不在MouseMove范围内,则不会触发它.)

Edit
Finally found out, why it's happening. It seems like it uses the MouseLeave event to set the property to False and MouseLeave event won't fire until the mouse button is released (that's why SledgeHammer used MouseMove event instead). If you have just one button (any control) on the form/window then you can use the code from the accepted answer, but if you got a standalone control (user/custom control), like I do then you can't use it (MouseMove won't trigger if outside of it).

如果有人能够在用户/自定义控件上使用更好的答案,那么请执行您应该做的...
我真的无法使其正常工作-必须对其进行处理:)

If anyone got better answer, usable on user/custom controls, then do what you should do...
I really can't make it work - must deal with it :)

推荐答案

在XAML中是不可能的,仅因为IsMouseOver属性的行为与您期望的不同(尽管它可以正确运行).解决方法很简单...只需添加以下代码,然后绑定到IsMouseOverEx而不是IsMouseOver.

Not possible in XAML only because the IsMouseOver property behaves in a different way that you expect (although its operating correctly). The workaround is simple... just add the following code and bind to IsMouseOverEx instead of IsMouseOver.

    public static readonly DependencyProperty IsMouseOverExProperty = DependencyProperty.Register("IsMouseOverEx", typeof(bool),
        typeof(xxx), new FrameworkPropertyMetadata(false));

    public bool IsMouseOverEx
    {
        get
        {
            return (bool)GetValue(IsMouseOverExProperty);
        }

        set
        {
            SetValue(IsMouseOverExProperty, value);
        }
    }

    private void xxx_MouseLeave(object sender, MouseEventArgs e)
    {
        IsMouseOverEx = false;
    }

    private void xxx_PreviewMouseMove(object sender, MouseEventArgs e)
    {
        Point pt = e.GetPosition(this);

        if ((pt.X < 0.0) || (pt.Y < 0.0) || (pt.X >= ActualWidth) || (pt.Y >= ActualHeight))
            IsMouseOverEx = false;
        else
            IsMouseOverEx = true;
    }

这篇关于WPF-即使鼠标不在上方,IsMouseOver仍为true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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