Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>basically, you have no solution except using a TemplateColumn and managing every single mouseEvent yourself.</p> <p>Explanation :</p> <p>click = <code>mouseDown</code> + <code>MouseUp</code>, right. so your button needs to be able to get the mouseDown + the MouseUp event.</p> <p>BUT...</p> <p>by default, wpf's DataGrid has its rows handling the <code>MouseDown</code> Event so as to select the one you do the <code>mouseDown</code> on (to confirm: <code>mouseDown</code> on a cell and hold the mouseButton down, you'll see that the row is selected <em>BEFORE</em> you release the button).</p> <p>So basically, the <code>MouseDownEvent</code> is Handled before it reaches the button, preventing you to be able to use the <code>Click</code> event on the button</p> <p>Microsoft tell us in their doc that in such cases, we should turn to the <em>Preview</em> kind of event, but this cannot apply to click event since there is no way you could have a <em>previewClickEvent</em></p> <p>So the only solution I can see for you is to listen to both <code>PreviewMouseDown</code> <strong>and</strong> <code>PreviewMouseUp</code> on your button and simulating a click from them yourself</p> <p>something a bit like this :</p> <pre><code>Button myButton = new Button(); bool mouseLeftButtonDownOnMyButton; myButton.PreviewMouseLeftButtonDown += (s, e) =&gt; { mouseLeftButtonDownOnMyButton = true; }; myButton.PreviewMouseLeftButtonUp += (s, e) =&gt; { if (mouseLeftButtonDownOnMyButton) myButton.RaiseEvent( new RoutedEventArgs(Button.ClickEvent,myButton)); mouseLeftButtonDownOnMyButton = false; }; myButton.Click += myButtonCLickHandler; </code></pre> <p>(of course, you'd need to translate that in your xaml template)</p> <p><strong>NB:</strong> this is not complete, you should also take care of the cases when the user does a mouseDown on the button but moves the mouse out of the button before doing the mouseup (in wich case you should reset the <code>mouseLeftButtonDownOnMyButton</code> flag). The best way would probably be to reset the flag in a general mouseUpEvent (on the window level for instance) instead of in the button's one.</p> <p><strong>Edit:</strong> the above code lets you manage the Click event as well and have only one code for both the real and simulated click events (hence the use of the RaiseEvent method), but if you don't need this, you could directly put your code in the PreviewMouseUp section as well of course.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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