How to bind a Command to double-click on a row in DataGrid

天涯浪子 提交于 2019-11-30 03:38:33

问题


I have developed a WPF UserControl that is intended to be used as a pick list as follows:

  • A DataGrid bound to a CollectionView of entities (e.g. of Employees)
  • A TextBox above the DataGrid that can be used to filter items displayed in the DataGrid.

I want to expose a Command that will be executed when the user double-clicks on a row in the DataGrid. The container can then react to this by doing something with the SelectedItem in the DataGrid.

So far I've tried to handle the double-click as follows:

<DataGrid IsReadOnly="True">
    <DataGrid.InputBindings>
        <MouseBinding MouseAction="LeftDoubleClick" Command="... />
    </DataGrid.InputBindings>
...

However the double-click event still fires when the user clicks in the DataGrid header. I'd like to be able to limit it so that the Command is only executed when the double click is in the body of the DataGrid. Is there a declarative way to do this?

UPDATE

I'm just getting to grips with WPF and MVVM, and am really looking for guidance on how to implement low-level reusable components like this. Any general advice will also be gratefully received and upvoted. As it stands, I'm assuming I will want this UserControl to:

  • Expose a dependency property "SelectedItem" that is bound to the DataGrid's SelectedItem

  • Expose a RoutedEvent "ItemDoubleClick" or similar that is fired when the user double-clicks on a row.

  • Implement ICommandSource and call CommandHelpers.ExecuteCommandSource(this) from the row double-click event handler.


回答1:


If code behind is not a problem:

<DataGrid.RowStyle>
    <Style TargetType="{x:Type DataGridRow}">
        <EventSetter Event="Loaded" Handler="Row_Loaded"/>
    </Style>
</DataGrid.RowStyle>
private void Row_Loaded(object sender, RoutedEventArgs e)
{
    var row = sender as DataGridRow;
    row.InputBindings.Add(new MouseBinding(MyCommands.MyCommand,
            new MouseGesture() { MouseAction = MouseAction.LeftDoubleClick }));
}



回答2:


You can simply put the DataGrid into a Grid and define your InputBindings in the Grid. In the canExecute-definition, you should check, if a row is selected. That works for the KeyBinding as well, for example a custom Delete-Command.

<Grid>
        <Grid.InputBindings>
            <MouseBinding MouseAction="LeftDoubleClick" Command="... />
        </Grid.InputBindings>
        <DataGrid IsReadOnly="True">
        ...
</Grid>


来源:https://stackoverflow.com/questions/5808616/how-to-bind-a-command-to-double-click-on-a-row-in-datagrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!