mHelpMe
mHelpMe

Reputation: 6668

WPF Datagrid Double Click Cell MVVM Design

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

Upvotes: 2

Views: 23748

Answers (5)

K.K.Srinivasan
K.K.Srinivasan

Reputation: 69

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="{Binding DoubleClickCommand}"/>
       </i:EventTrigger>
</i:Interaction.Triggers>

That's it. Hope it helps you:)

Upvotes: 6

Sungjin Jung
Sungjin Jung

Reputation: 3

Let assume

  1. DataGrid as MyGridDataList
  2. Griddata of selected row as MyGridData
public Icollection<MyGridData> MyGridTable {get; private set;}
public MyGridData MyData {get; private set;}

public MyClass()
{
    MyGridData = new MyGridData();
    /// assume datas had set
    MyGridTable = new ObservableCollection<MyGridData>();
    MyGridTable.Add(MyGridData); //more then 2 datas
}

Push MyGridTable 2 or more MyGridData to find which selected row MyGridData found. Then, xaml side

<!--abstract... your GridData tag -->
<DataGrid SelectedItem="{Binding MyData}" ItemSource="{Binding MyGridTable}"/>
<!--And put Trigger Code ABOVE Reply-->

Lanch App and Double Click DataGrid, and then could find Correspond MyData in MyGridTable selected Row.

Upvotes: 0

bobah75
bobah75

Reputation: 3560

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);
    }

Upvotes: 29

user2999882
user2999882

Reputation: 11

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);
        }
    }
}

Upvotes: 1

aliceraunsbaek
aliceraunsbaek

Reputation: 665

I'v used the MouseDoubleClick:

    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);
            }
        }
    }

When you double-click a data grid, the row is also selected, so I simply find the selected item and use it.

Upvotes: 0

Related Questions