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

后端 未结 3 1963
旧时难觅i
旧时难觅i 2020-12-16 02:08

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 wan

相关标签:
3条回答
  • 2020-12-16 02:18

    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
        }
    }
    
    0 讨论(0)
  • 2020-12-16 02:29

    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.
    }
    

    }

    0 讨论(0)
  • 2020-12-16 02:35

    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
    }
    
    0 讨论(0)
提交回复
热议问题