2

I have a WPF DataGrid with some styling, properties and events.
I want to override its OnMouseLeftButtonDown and OnMouseLeftButtonUp events to do something.
How do I do this?
Thanks!

amitairos
  • 2,907
  • 11
  • 50
  • 84
  • 1
    Are you sure you need an override rather than just a subscription? What do you need this for? What have you tried and what is the problem? – H.B. Aug 14 '11 at 15:55
  • @H.B. I am trying to solve the problem asked here: http://stackoverflow.com/questions/3767708/wpf-drag-and-drop-from-a-listbox-that-has-selectionmode-extended and am trying the solution given. Apparently it doesn't solve it (even when I did what ChrisF suggested). Any ideas? – amitairos Aug 14 '11 at 19:46

2 Answers2

4

Create your own class based on DataGrid and add the event handlers you need. Then in your XAML use your DataGrid rather than the "normal" one.

public class MyDataGrid : DataGrid
{
    // Your overrides here
}

And in XAML:

<Window x:Class="MyProject.MyNamespaceMyClass"
        ....
        xmlns:local="clr-namespace:MyProject.MyNamespace">

    ....
    <local:MyDataGrid ... />
    ....

</Window>
ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • Thank you. I am trying to solve the problem asked here: http://stackoverflow.com/questions/3767708/wpf-drag-and-drop-from-a-listbox-that-has-selectionmode-extended and am trying the solution given. Apparently it doesn't solve it. Any ideas? – amitairos Aug 14 '11 at 19:36
  • Brilliant - helps me use ComponentModel attributes as grid column headings by overidding OnAutoGeneratingColumn within MyDataGrid.cs – Greg Trevellick Jan 11 '18 at 13:17
0

Add an event and in the event put e.Handled = true if you want to mark it as handled. Be aware ordering of event bubbling is different for down compared to up and there is a preview. Or you can override the method. I am just more comfortable with events. If you override the down event then the up event might not fire - not sure but it is something to test for.

H.B.
  • 166,899
  • 29
  • 327
  • 400
paparazzo
  • 44,497
  • 23
  • 105
  • 176