我有一个由列表视图构成的可滚动时间轴。 当我的鼠标放在列表视图上时。
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.
为此:
这是它的外观:
<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光标更改
发布评论