amitairos
amitairos

Reputation: 2967

WPF override DataGrid

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!

Upvotes: 2

Views: 1431

Answers (2)

ChrisF
ChrisF

Reputation: 137148

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>

Upvotes: 4

paparazzo
paparazzo

Reputation: 45096

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.

Upvotes: 0

Related Questions