Search code examples
wpfxamldatagrideventtrigger

Scrolling the Datagrid firing mouse double click event


I have a DataGrid and I want to show another form when the row is double clicked but this event is firing even if I am scrolling the datagrid. How to make it fire only when the row is double clicked?

I am using MVVM pattern.

<DataGrid Name="dgScopeRecords" Grid.Row="1" Grid.ColumnSpan="3" IsReadOnly="True" ItemsSource="{Binding Model.TableScopeRecords}" SelectedIndex="{Binding Model.SelectedIndex}" Margin="0,10,0,0" AutoGenerateColumns="False" SelectionMode="Single">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseDoubleClick">
            <i:InvokeCommandAction Command="{Binding ViewScopeRecordCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>

    <DataGrid.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#dca188"/>
    </DataGrid.Resources>

    <DataGrid.Columns>
        <DataGridTextColumn Header="{x:Static Resources:Translations.RecordsInspection_ColumnScope}" Binding="{Binding Scope.ScopeName}" Width="250"/>
        <DataGridTextColumn Header="{x:Static Resources:Translations.RecordsInspection_ColumnScopeType}" Binding="{Binding Scope.ScopeType.ScopeTypeName}" Width="100"/>
    </DataGrid.Columns>
</DataGrid>

Solution

  • I can only assume that your Interaction.Triggers are causing your problem. I accept that you want to 'convert' that event into a Command, but if you implement a standard MouseDoubleClick handler instead (just temporarily), you will see that scrolling does not fire the handler at all.

    You can still convert that event to a Command in your own custom Attached Property. They are relatively simple to create and you can find out how to do this from the Attached Properties Overview page at MSDN.