WPF ListView光标更改

时间:2010-06-29 19:59:54

标签: wpf cursor

我有一个可滚动的时间轴,由列表视图构成。 当我的鼠标聚焦在列表视图上时。

使用代码

,光标是张开的手
<ControlTemplate.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
        <Setter Property="Cursor" Value="openHand.cur"/>
    </Trigger>                    
</ControlTemplate.Triggers>

但我很想知道。如果在列表视图上按下鼠标左键,我可以做些什么。 如果是,那么将光标更改为闭合的手? 提前谢谢!

1 个答案:

答案 0 :(得分:2)

WPF没有“IsMouseLeftButtonDown”属性,但您可以创建自己的附加属性来执行此操作,然后触发它。这往往比将MouseLeftButtonDown事件处理程序添加到单个控件要清晰得多。

要做到这一点:

  1. 为IsMouseLeftButtonDown(以及其他按钮)创建继承的附加属性
  2. 创建“已启用”附加属性以自动设置所需的事件处理程序。
  3. 直接在您的控件或任何包含控件上设置“已启用”属性。
  4. 在Trigger或MultiTrigger中使用“IsMouseLeftButtonDown”属性
  5. 以下是它的外观:

    <Window ...
            local:MouseExtensions.Enabled="true" />  <!-- Set the handlers -->
      ...
      <ControlTemplate.Triggers>
        <Trigger Property="IsMouseOver" Value="True" >
          <Setter Property="Cursor" Value="openHand.cur"/>
        </Trigger>
        <MultiTrigger>
          <MultiTrigger.Conditions>
            <Condition Property="IsMouseOver" Value="True" />
            <Condition Property="local:MouseExtensions.IsMouseLeftButtonDown" Value="True" />
          </MultiTrigger.Conditions>
          <Setter Property="Cursor" Value="closedHand.cur" />
        </MultiTrigger>
      </ControlTemplate.Triggers>
    

    以下是附加属性的实现方式:

    public class MouseExtensions : DependencyObject
    {
      // IsMouseLeftButtonDown
      public static bool GetIsMouseLeftButtonDown(DependencyObject obj) { return (bool)obj.GetValue(IsMouseLeftButtonDownProperty); }
      public static void SetIsMouseLeftButtonDown(DependencyObject obj, bool value) { obj.SetValue(IsMouseLeftButtonDownProperty, value); }
      public static readonly DependencyProperty IsMouseLeftButtonDownProperty = DependencyProperty.RegisterAttached("IsMouseLeftButtonDown", typeof(bool), typeof(MouseExtensions), new FrameworkPropertyMetadata
      {
        Inherits=true,
      });
    
    
      // IsMouseMiddleButtonDown
      public static bool GetIsMouseMiddleButtonDown(DependencyObject obj) { return (bool)obj.GetValue(IsMouseMiddleButtonDownProperty); }
      public static void SetIsMouseMiddleButtonDown(DependencyObject obj, bool value) { obj.SetValue(IsMouseMiddleButtonDownProperty, value); }
      public static readonly DependencyProperty IsMouseMiddleButtonDownProperty = DependencyProperty.RegisterAttached("IsMouseMiddleButtonDown", typeof(bool), typeof(MouseExtensions), new FrameworkPropertyMetadata
      {
        Inherits=true,
      });
    
      // IsMouseRightButtonDown
      public static bool GetIsMouseRightButtonDown(DependencyObject obj) { return (bool)obj.GetValue(IsMouseRightButtonDownProperty); }
      public static void SetIsMouseRightButtonDown(DependencyObject obj, bool value) { obj.SetValue(IsMouseRightButtonDownProperty, value); }
      public static readonly DependencyProperty IsMouseRightButtonDownProperty = DependencyProperty.RegisterAttached("IsMouseRightButtonDown", typeof(bool), typeof(MouseExtensions), new FrameworkPropertyMetadata
      {
        Inherits=true,
      });
    
      // Enabled
      public static bool GetEnabled(DependencyObject obj) { return (bool)obj.GetValue(EnabledProperty); }
      public static void SetEnabled(DependencyObject obj, bool value) { obj.SetValue(EnabledProperty, value); }
      public static readonly DependencyProperty EnabledProperty = DependencyProperty.RegisterAttached("Enabled", typeof(bool), typeof(MouseExtensions), new PropertyMetadata
      {
        PropertyChangedCallback = (obj, e) =>
          {
            var element = (FrameworkElement)obj;
            if((bool)e.OldValue)
            {
              element.PreviewMouseDown -= Update;
              element.PreviewMouseUp -= Update;
              element.MouseEnter -= Update;
              element.MouseLeave -= Update;
            }
            if((bool)e.NewValue)
            {
              element.PreviewMouseDown += Update;
              element.PreviewMouseUp += Update;
              element.MouseEnter += Update;
              element.MouseLeave += Update;
            }
          }
      });
    
      private static void Update(object sender, MouseEventArgs e)
      {
        var element = (FrameworkElement)sender;
        bool inside = e.RoutedEvent!=Mouse.MouseLeaveEvent;
        SetIsMouseLeftButtonDown(element, inside && e.LeftButton==MouseButtonState.Pressed);
        SetIsMouseMiddleButtonDown(element, inside && e.MiddleButton==MouseButtonState.Pressed);
        SetIsMouseRightButtonDown(element, inside && e.RightButton==MouseButtonState.Pressed);
      }
    }
    

    工作原理:“Enabled”PropertyChangedCallback将“Update”方法添加为四个鼠标事件的处理程序。当其中一个事件发生时,将检查当前鼠标按钮状态,并在“Enabled”设置为true的元素上更新Is___ButtonDown属性。从那里,这些属性通过逻辑和可视树继承。但是,如果收到MouseLeave,则所有这些属性都设置为false,因为再次接收鼠标事件,直到鼠标再次位于设置为“Enabled”的元素上。