如何创建/处理IsMouseOverChanged

时间:2012-06-11 19:02:42

标签: wpf events properties mouse

我喜欢IsMouseDirectlyOverChanged的功能,但它只适用于最顶层的元素。我真正想要做的是监视控件的IsMouseOver属性,无论其z-index如何,并在修改IsMouseOver时接收通知。如果我拥有这个属性,我可以做INotifyPropertyChanged,但遗憾的是它归UIElement所有。仍在学习WPF的绳索,所以也许这很容易,但我不熟悉如何监视属于父类的属性。与IsMouseDirectlyOver不同,IsMouseOver没有附加更改的事件。

2 个答案:

答案 0 :(得分:2)

我看到有一个公认的解决方案,我只想分享我的意见。在我的情况下,我在Usercontrol中有一个Label,我需要对鼠标的反应结束。对我来说,MouseEnter和MouseLeave事件会触发,所以我可以处理它们。我在Code Behind中定义了标签(因为我以编程方式将其放在运行时)并且只添加了标签对象的标准事件。 IsMouseDirectlyOverChanged对我不起作用,因为我正在盘旋在Usercontrol上。如果存在另一个Control重叠,这也有效。所以也许这不适合你的情况,也许它确实如此:-)  
我也想粘贴xaml,但似乎我没有得到降价的东西: - )

...
    Private Sub Rectangle1_MouseEnter(sender As Object, e As System.Windows.Input.MouseEventArgs) Handles Rectangle1.MouseEnter, Rectangle2.MouseEnter
        Dim s As Rectangle = sender
        s.Fill = Brushes.Red
    End Sub

    Private Sub Rectangle1_MouseLeave(sender As Object, e As System.Windows.Input.MouseEventArgs) Handles Rectangle1.MouseLeave, Rectangle2.MouseLeave
        Dim s As Rectangle = sender
        s.Fill = Brushes.Green
    End Sub
...

BR, d

答案 1 :(得分:1)

您可以使用样式对IsMouseOver更改时做出反应。

<Window.Resources>
     <Style x:Key="RectStyle" TargetType="Rectangle">
          <Style.Triggers>
               <Trigger Property="IsMouseOver" Value="True">
                    <!-- react to property change here -->
               </Trigger>
          </Style.Triggers>
     </Style>
 </Window.Resources>

然后将样式附加到元素:

<Rectangle Style="{StaticResource RecStyle}" Width="100" Height="100" Fill="Black" />

修改

如果要从样式中调用代码,解决方案相当复杂。

首先,您必须创建一个自定义UserControl类并为其提供附加属性。每当附加属性发生更改时,您都可以使用属性更改回调。这是*.xaml.cs文件,只保留默认的.xaml文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ExampleApp
{
    /// <summary>
    /// Interaction logic for MouseOverWrapper.xaml
    /// </summary>
    public partial class MouseOverWrapper : UserControl
    {

        public static readonly DependencyProperty MouseIsOverProperty;

        static MouseOverWrapper()
        {
            MouseIsOverProperty = DependencyProperty.RegisterAttached(
                "MouseIsOver", typeof(bool), typeof(MouseOverWrapper),
                new FrameworkPropertyMetadata(false, OnMouseIsOverChanged));
        }

        public static bool GetMouseIsOver(UIElement element)
        {
            return (bool)element.GetValue(MouseIsOverProperty);
        }

        public static void SetMouseIsOver(UIElement target, bool value)
        {
            target.SetValue(MouseIsOverProperty, value);
        }

        private static void OnMouseIsOverChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            // handle code here. example:
            if ((bool)e.NewValue == true)
                ((Rectangle)d).Fill = Brushes.Yellow;
            else
                ((Rectangle)d).Fill = Brushes.Black;
        }

        public MouseOverWrapper()
        {
            InitializeComponent();
        }
    }
}

然后,您必须在自定义控件中包含要响应的元素,并使样式更改该自定义控件的附加属性。例如:

<Window x:Class="ExampleApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ExampleApp"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="RecStyle" TargetType="Rectangle">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="local:MouseOverWrapper.MouseIsOver" Value="True" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <local:MouseOverWrapper>
        <Rectangle Style="{StaticResource RecStyle}" Width="100" Height="100" Fill="Black" />
    </local:MouseOverWrapper>
</Window>

将所有代码添加到OnMouseOverIsChanged函数中,这应该可以。