Hello,
I am new to infragistics, I need help in hooking a xamDataGrid row Double Click Event to a ViewModel Command. I have done a lot of research and could not find a clear and direct answer.
Thank you
Hello User101,
Thank you for your update. I am glad you found a method that works for you on this matter.
Sincerely,AndrewAssociate Developer
Hey Andrew,
Thank you for your response.
This code works and takes care of all issues:
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick"> <i:InvokeCommandAction Command="{Binding myCommand}" CommandParameter="{Binding ElementName=igdatagrid, Path=SelectedDataItem}"/> </i:EventTrigger> </i:Interaction.Triggers>
The actual data record objects in the XamDataGrid are not visual elements, and so you won't really be able to place a double-click directly on them, but you could do this via a Style for DataRecordPresenter, which is the element that presents the actual data records. The issue with this is that I'm not sure of any sort of way to actually pass an event to a command via a Style for DataRecordPresenter that doesn't include the control template for that particular element. For this reason, if you are looking to restrict this to only happen on click of a DataRecordPresenter, I would recommend that you go into the DataPresenterGeneric_Express.xaml file commonly found at the following directory C:\Program Files (x86)\Infragistics\<your version here>\WPF\DefaultStyles\DataPresenter, and find the default style for DataRecordPresenter. Include this style in your project.
This default style will include the default template for the DataRecordPresenter. The parent element of this template is a CardPanel, which I would recommend that you place the Interaction.Triggers on, like before. Unfortunately, this CardPanel doesn't have a direct "double click" event, but it does have a PreviewMouseDown event which you can check the event arguments of to find out if the mouse was double-clicked. This property is ClickCount, which will return 2 if a double click happened.
The issue now is how to pass the event arguments of the PreviewMouseDown event back to your command. This is not easily done, but after some research, I found the following article that may help you with this: http://weblogs.asp.net/alexeyzakharov/silverlight-commands-hacks-passing-eventargs-as-commandparameter-to-delegatecommand-triggered-by-eventtrigger. This article shows how to pass event arguments back to a command, essentially using a derivation of the InvokeCommandAction used in the original sample that I had sent you.
I have also attached an updated version of the original sample project to demonstrate passing the event arguments back to a command in the ViewModel using the classes mentioned in the above article, and using the default style of the DataRecordPresenter.
I hope this helps you. Please let me know if you have any other questions or concerns on this matter.
Thank you Andrew for your response.
I have tested the code. Is there a way to restrict the double click to the data records only and not any where where on xamdatagrid.
Thank you for your post.
To hook a XamDataGrid double click event to a ViewModel command, I would recommend pulling in the Prism library, as it's DelegateCommand class may help you - especially if you are looking to handle the command execution in the ViewModel. This can also be done using a simple ICommand, but the Execute method would exist in the ICommand's class in that case.
Once you have created your command, you can include the System.Windows.Interactivity assembly, and use its Interaction.Triggers collection to attach an EventTrigger to the XamDataGrid. If you target the MouseDoubleClick event of the XamDataGrid using this EventTrigger, you can then place an InvokeCommandAction to target your ViewModel command. The code to do this would look like the following where the 'behaviors' namespace targets the System.Windows.Interactivity assembly:
<ig:XamDataGrid><behaviors:Interaction.Triggers><behaviors:EventTrigger EventName="MouseDoubleClick"><behaviors:InvokeCommandAction Command="{Binding MyCommand}"/></behaviors:EventTrigger></behaviors:Interaction.Triggers>
Now, if you are looking to actually retrieve the record that was clicked on execution of the command, I would recommend that you use the Mouse.DirectlyOver attached property. This will retrieve the element that the mouse is directly over. If you use this with the Infragistics.Windows.Utilities class and its GetDescendantFromType/GetAncestorFromType methods, you can retrieve a DataRecordPresenter element from the visual tree of the element that was clicked. The code to retrieve this DataRecordPresenter would look like the following:
var y = Utilities.GetDescendantFromType(x as DependencyObject, typeof(DataRecordPresenter), false);var z = Utilities.GetAncestorFromType(x as DependencyObject, typeof(DataRecordPresenter), false);
Once you have this, you can retrieve the actual DataRecord element from the DataRecordPresenter's Record property.
I have attached a sample project to demonstrate the above. I hope this helps you.
Please let me know if you have any other questions or concerns on this matter.