How do i handle cell double click event on WPF DataGrid, equivalent to windows DataGrid's Events?
Asked Answered
G

4

13

As you know, in windows C#'s gridview, if we want to handle a click/double click event on cell then there are events like CellClick, CellDoubleClick, etc.

So, i wanna do same like as windows gridview with WPF DataGrid. I have searched so far but neither answer is applicable nor useful. Some of them says use the MouseDoubleClick event but, in this event, we have to check for each row as well as item in that row, so it is time consuming to check every cell for data and timing is most important here.

My DataGrid is bounded to DataTable and AutoGeneratedColumn is False. If your answer is based on AutoGeneratedColumn=True then it is not possible. Even, i 'm changing the styles of datagrid cell according to data, so there is no way to change AutoGeneratedColumn property.

A Cell Clicking/Double Clicking event should be as faster as windows grid's event. If it is possible then tell me how, and if not, then what is the alternative to do it?

Please Help Me.....

Thanks a lot....

Gaberdine answered 28/1, 2013 at 7:50 Comment(0)
H
19

I know this may be a little late to the party, but this might be useful to someone else down the road.

In your MyView.xaml:

<DataGrid x:Name="MyDataGrid" ...>
    <DataGrid.Resources>
        <Style TargetType="{x:Type DataGridCell}">
            <EventSetter Event="MouseDoubleClick" Handler="DataGridCell_MouseDoubleClick"/>
        </Style>
    </DataGrid.Resources>

    <!-- TODO: The rest of your DataGrid -->
</DataGrid>

In your MyView.xaml.cs:

private void DataGridCell_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    var dataGridCellTarget = (DataGridCell)sender;
    // TODO: Your logic here
}
Hornback answered 10/11, 2017 at 17:11 Comment(1)
Excellent answerChrysanthemum
A
5

An alternative way would to be define a DataGridTemplateColumn instead of using the predefined columns like DataGridCheckBoxColumn, DataGridComboBoxColumn and then add an event handler to the UI element defined in the data template.

Below I have defined a MouseDown event handler for a TextBlock Cell.

<DataGrid AutoGenerateColumns="False">
    <DataGrid.Columns>

        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock MouseDown="TextBlock_MouseDown"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

In the Code behind file:

private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
    TextBlock block = sender as TextBlock;
    if (block != null)
    {
        // Some Logic
        // block.Text
    }
}
Anthroposophy answered 28/1, 2013 at 8:32 Comment(3)
Thanks but where to write this code because i don't have code in cs file file except connection to database and i 'm changing the style of particular grid according to data at that position, so where to define this code that i don't know...even though 'm trying to do now.....thanks for reply...Gaberdine
You have to add the code in DataGrid.Columns section. I have updated my answer....Anthroposophy
How are you generating the DataGrid columns in code behind or in xaml?Anthroposophy
D
3

I know coding WPF is sometimes a PITA. Here you would have to handle the MouseDoubleClick event anyway. Then search the source object hierarchy to find a DataGridRow and do whatever with it.

UPDATE: Sample code

XAML

<dg:DataGrid MouseDoubleClick="OnDoubleClick" />

Code behind

private void OnDoubleClick(object sender, MouseButtonEventArgs e)
{
    DependencyObject source = (DependencyObject) e.OriginalSource;
    var row = GetDataGridRowObject(source);
    if (row == null)
    {
         return;
    }
    else
    {
        // Do whatever with it
    }
    e.Handled = true;
}

private DataGridRow GetDataGridRowObject(DependencyObject source)                               
{
    // Write your own code to recursively traverse up via the source
    // until you find a DataGridRow object. Otherwise return null.
}

}

Dashed answered 28/1, 2013 at 8:6 Comment(0)
P
0

I have used something like this:

<DataGrid.InputBindings>
    <MouseBinding Gesture="LeftDoubleClick" Command="{Binding ShowOverlay}" CommandParameter="{Binding Parameter}" />
</DataGrid.InputBindings>

And handle my commands in my View Model.

Pawsner answered 3/6, 2021 at 21:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.