Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>To do this:</p> <ol> <li>Create inherited attached properties for IsMouseLeftButtonDown (and for the other buttons too)</li> <li>Create an "Enabled" attached property to automatically set the required event handlers.</li> <li>Set the "Enabled" property directly on your control, or on any containing control.</li> <li>Use the "IsMouseLeftButtonDown" property in a Trigger or MultiTrigger</li> </ol> <p>Here is how it might look:</p> <pre><code>&lt;Window ... local:MouseExtensions.Enabled="true" /&gt; &lt;!-- Set the handlers --&gt; ... &lt;ControlTemplate.Triggers&gt; &lt;Trigger Property="IsMouseOver" Value="True" &gt; &lt;Setter Property="Cursor" Value="openHand.cur"/&gt; &lt;/Trigger&gt; &lt;MultiTrigger&gt; &lt;MultiTrigger.Conditions&gt; &lt;Condition Property="IsMouseOver" Value="True" /&gt; &lt;Condition Property="local:MouseExtensions.IsMouseLeftButtonDown" Value="True" /&gt; &lt;/MultiTrigger.Conditions&gt; &lt;Setter Property="Cursor" Value="closedHand.cur" /&gt; &lt;/MultiTrigger&gt; &lt;/ControlTemplate.Triggers&gt; </code></pre> <p>Here's how the attached property might be implemented:</p> <pre><code>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) =&gt; { 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 &amp;&amp; e.LeftButton==MouseButtonState.Pressed); SetIsMouseMiddleButtonDown(element, inside &amp;&amp; e.MiddleButton==MouseButtonState.Pressed); SetIsMouseRightButtonDown(element, inside &amp;&amp; e.RightButton==MouseButtonState.Pressed); } } </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload