WPF ListView光标更改 [英] WPF ListView Cursor Change

查看:94
本文介绍了WPF ListView光标更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a scrollable timeline that is made from list views. When my mouse is focused over the list view.

光标是使用代码的张开手

The cursor is a open hand using the code

<ControlTemplate.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
        <Setter Property="Cursor" Value="openHand.cur"/>
    </Trigger>                    
</ControlTemplate.Triggers>

但我想知道。如果在列表视图上按下鼠标左键,我可以做些什么吗?
是否将光标更改为闭合手形?
预先感谢!

but I was wondering. Is there something I can do if the left mouse button is pressed over the list view. if it is then change the Cursor to a closed hand? Thanks in advance!

推荐答案

WPF没有 IsMouseLeftButtonDown属性,但是您可以创建自己的属性附加属性来执行此操作,然后在其上触发。与向单个控件添加MouseLeftButtonDown事件处理程序相比,这样做往往更清洁。

WPF doesn't have a "IsMouseLeftButtonDown" property, but you can create your own attached property to do this, then trigger on it. This tends to be much cleaner than adding MouseLeftButtonDown event handlers to individual controls.

为此:


  1. 为IsMouseLeftButtonDown(以及其他按钮)创建继承的附加属性

  2. 创建一个 Enabled附加属性以自动设置所需的事件处理程序。

  3. 直接在控件或任何包含控件的控件上设置启用属性。

  4. 在触发器或MultiTrigger中使用 IsMouseLeftButtonDown属性

  1. Create inherited attached properties for IsMouseLeftButtonDown (and for the other buttons too)
  2. Create an "Enabled" attached property to automatically set the required event handlers.
  3. Set the "Enabled" property directly on your control, or on any containing control.
  4. Use the "IsMouseLeftButtonDown" property in a Trigger or MultiTrigger

这是它的外观:

<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>

以下是附加财产的实施方式:

Here's how the attached property might be implemented:

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);
  }
}

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

How it works: The "Enabled" PropertyChangedCallback adds the "Update" method as a handler for four mouse events. When one of these events occurs, the current mouse button state is checked and the Is___ButtonDown properties are updated on the element where "Enabled" was set to true. From there, these properties are inherited down through the logical and visual tree. However if MouseLeave is received all these properties are set to false, since no mouse events will be received again until the mouse is once again over the element that "Enabled" was set on.

这篇关于WPF ListView光标更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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