我有一个可滚动的时间轴,由列表视图构成。 当我的鼠标聚焦在列表视图上时。
使用代码
,光标是张开的手<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Cursor" Value="openHand.cur"/>
</Trigger>
</ControlTemplate.Triggers>
但我很想知道。如果在列表视图上按下鼠标左键,我可以做些什么。 如果是,那么将光标更改为闭合的手? 提前谢谢!
答案 0 :(得分:2)
WPF没有“IsMouseLeftButtonDown”属性,但您可以创建自己的附加属性来执行此操作,然后触发它。这往往比将MouseLeftButtonDown事件处理程序添加到单个控件要清晰得多。
要做到这一点:
以下是它的外观:
<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”的元素上。