Álvaro García
Álvaro García

Reputation: 19386

how to disabled double click event when I use the navigation arrows in the vertical scroll?

I have a dataGrid in my wpf application and I use the double click mouse event to display details of the element selected in the dataGrid.

However, I have a problem when I use the navigation arrows of the vertical scroll, because is very common to click the mouse button rapidly and then the event of the double is fired, but I only want to show details when I do double click in the row of the datagrid, not in the navigation arrow.

How can I disabled the double click event when I click in the navigation arrow?

Thanks.

EDIT: I find a solution that uses mouse input bindings and a gesture. It seems that the input bindings only fire the command if I double click in the rows zone, not in the vertical or horizontal scrollbars. However, if the table only has a few rows and you see the grey zone below the last row and I double click in this zone the event is fire, so I would have the same problem, but at least solve the problem when I use the scroll bars.

The solution is this:

AXML

<DataGrid>
    <DataGrid.InputBindings>
                <MouseBinding Gesture="LeftDoubleClick" Command="{Binding DgdComponentesMouseDoubleClickCommand}" CommandParameter="{Binding ElementName=dgdComponentes, Path=SelectedItems, Mode=OneWay}"/>
                <KeyBinding Key="Enter" Command="{Binding DgdComponentesMouseDoubleClickCommand}" CommandParameter="{Binding ElementName=dgdComponentes, Path=SelectedItems, Mode=OneWay}"/>
    </DataGrid.InputBindings>
</DataGrid>

VIEWMODEL

private RelayCommand<object> _dgdComponentesMouseDoubleClickCommand;
public RelayCommand<object> DgdComponentesMouseDoubleClickCommand
{
    get { return _dgdComponentesMouseDoubleClickCommand ?? (_dgdComponentesMouseDoubleClickCommand = new RelayCommand<Object>(dgdComponentesMouseDoubleClick)); }
}

Upvotes: 2

Views: 1429

Answers (1)

Anatoliy Nikolaev
Anatoliy Nikolaev

Reputation: 22702

In this case, the event handler should be placed directly on the DataGridRow. Something like this:

Style

<Style BasedOn="{StaticResource {x:Type DataGridRow}}" TargetType="{x:Type DataGridRow}">
    <EventSetter Event="MouseDoubleClick" Handler="SampleDataGrid_MouseDoubleClick" />
</Style>

Handler

private void SampleDataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    MessageBox.Show("Out some info...");
}   

I have tried to block the event ScrollViewer (e.Handled = true), but it is useless. The event is triggered at the same time, and in both cases the source DataGrid. Apparently, this is because the ScrollViewer is part of the DataGrid.

Upvotes: 2

Related Questions