WPF Datagrid Double Click Cell MVVM Design

20,741

Solution 1

Instead of double-clicking on the cell you may double-click on the grid

<DataGrid.InputBindings>
    <MouseBinding Gesture="LeftDoubleClick" 
    Command="{Binding Edit}" 
    CommandParameter="{Binding ElementName=UsersDataGrid, Path=SelectedItem}" />
</DataGrid.InputBindings>

In ViewModel

    public ICommand Edit { get; private set; }

 Edit = new RelayCommand(EditUser, x => _isAdmin);



 private static void EditUser(object usr)
    {
        if (!(usr is User))
            return;

        new UserEditorViewModel(usr as User);
    }

Solution 2

We can do this in two ways,

a) By using Dependency property b) By adding System.Windows.Interactivity.dll.

Usually I prefer second way.

step 1: Implement ICommand interface in your view model class file.

step 2: Define your Command,

public ICommand DoubleClickCommand
{
//Do your code
}

step 2: add the above said .dll into your corresponding solution's xaml file.

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

step 3: Inside the Datagrid tag, use the below code to implement InvokeCommandAction Class

<i:Interaction.Triggers>
       <i:EventTrigger EventName="MouseDoubleClick">
             <i:InvokeCommandAction Command="DoubleClickCommand"/>
       </i:EventTrigger>
<i:Interaction.Triggers>

That's it. Hope it helps you:)

Solution 3

private void DataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs eventArgs)
{
    if (sender == null) return;
    if (eventArgs.ButtonState != MouseButtonState.Pressed) return; //only react on pressed

    var dataGrid = sender as DataGrid;
    if (dataGrid == null || dataGrid.SelectedItems == null) return;

    if (dataGrid.SelectedItems.Count == 1)
    {
        var simplePension = dataGrid.SelectedItem as ISimplePension;
        if (simplePension != null)
        {
            DataFetcherHolder.DataFetcher.SelectPension(simplePension);
            Execute(EditSelectedPensionFunction);
        }
    }
}
Share:
20,741
mHelpMe
Author by

mHelpMe

Updated on February 22, 2022

Comments

  • mHelpMe
    mHelpMe about 2 years

    I have a WPF application that contains a datagrid. It is bound to my List object "Orders" shown below.

    public class OrderBlock
    {
      public Settings setting;
      public List<Order> orders;
    }
    public class Order
    {
      public int Amount;
      public string OrderID;
      public string OrderIDDup;
      public string Name;
      public string NameDup;
      public bool DupIDs;
      // and some string, int fields
    }
    

    For reasons out of my control it is possible that there can be more than one OrderID, hence the OrderIDDup property. My datagrid by default shows just the OrderID and not the OrderIDDup.

    What I would like to do is for the user to be able to click on the cell ID and for another window to load to show them the other ID as well as the two names and let them choose which ID should be used.

    I have been reading that the WPF DataGrid doesn’t support this functionality of double clicking on a cell. So I am a bit lost as how i should start going about this issue. The other issue I can see is that as I am trying (being the operative word) to use a MVVM design how would this kind of event be exposed to my view model?

    Also is this the best way to go about showing such information.

    Any help would be great, Thanks, M