Saturday 15 September 2012

c# - How to catch mouse movement when left mouseButton is pressed on a button control and released on different button control? -


I have created custom user control in WPF, which is shown in the enclosed image, I use the mouse "8" and the second button Hold the mouse button on like: leave the button "1" and the mouse left button. Now I want to get the two buttons to click on the "8" button and release the "1" button. To bring the mouse to the event I have registered PreviewMouseLeftButtonDown to get the mouse down event and the preview-mouse life buttonup. But when I click on "8" and take the "1" release button in both events, then I get the same "8" button, please let anyone know how I can get it.

  Private Toggle Button _ Start Button; Private toggle button _and button; Private Zero Tb_PreviewMouseLeftButtonDown (Object Sender, MouseButtonEventArgs E) {_startButton = Sender as Toggleton; } Private Zero tb_PreviewMouseLeftButtonUp (Object Sender, MouseButtonEventArgs e) {_endButton = Sender as Toggleton; If (_endButton! = Null & StartButton! = Null) {string start = _startbutton.Content.ToString (); String end = _endButton.Content.ToString (); If (! Start.Equals (End) ToggleButton (_endButton); }}    

This behavior is due to the fact that the mouse is being captured For example, you have the following layout:

UPDATE

>

  Lt; Window x: Class = "WpfApplication24.MainWindow" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns: x = "http: // schema .microsoft.com / winfx / 2006 / Xaml "title =" main window "height =" 350 "width =" 525 "& gt; & Lt; StackPanel & gt; & Lt; Button content = "B1" preview mouse leftbootnipe = "buttonpreviewupsmouthbootnubt" / & gt; & Lt; Button content = "b2" /> & Lt; / StackPanel & gt; & Lt; / Window & gt;   

Its PreviewMouseLeftButtonUp handler you must execute the following code:

  private void Button_PreviewMouseLeftButtonUp_1 (object sender, MouseButtonEventArgs e) {var result = VisualTreeHelper.HitTest ( This, E. Getpzin (this)); }   

Note that you should use the hitst element for the element, which is the normal guardian for both of your buttons (for example - this is the main window)

In the result VisualHeat Asset You can see the element under the cursor.

After that you can check it using VisualTreeHelper this child your button, or try the following approaches:

1) Create a flag attached property:

  public static bool GetIsHitTestTarget (DependencyObject obj) {return (bool) obj.GetValue (IsHitTestTargetProperty); } Public static zero SetIsHitTestTarget (dependency object obj, bool value) {obj.SetValue (IsHitTestTargetProperty, value); } Public static Readonly DependencyProperty IsHitTestTargetProperty = DependencyProperty.RegisterAttached ("IsHitTestTarget", typeof (bool), typeof (MainWindow), New PropertyMetadata (wrong));   

2) Set it for elements that you should find:

  & lt; Button content = "b1" local: main window ISHTSTSTA target = "true" previewMoice LeftBootNewPop = "buttonPreviewWebMootBootnOut" / & gt; & Lt; Button content = "b2" local: MainWindow.IsHitTestTarget = "true" />   

3) modify PreviewLeftButtonUp callback:

  private void Button_PreviewMouseLeftButtonUp_1 (object sender, MouseButtonEventArgs e) {DependencyObject result = null; VisualTreeHelperkHitTest (this, (o) = & gt; {if (GetIsHitTestTarget (o)) {result = O; HitTestFilterBehavior.Stop return;} return HitTestFilterBehaviorkContinue;}, (race) = & gt; HitTestResultBehavior.Stop, New PointHitTestParameters (e.GetPosition (this))); }    

No comments:

Post a Comment