Hi, I have been struggling for a few days now to find an answer for these questions.
first, I need to a be able to select an item on right click on the XamDataGrid. I did hope that my XAML
for ContextMenu would do the trick and I saw that the item that is highlighted when I right click on the
row to get the ContextMenu I open, would set the SelectedItems property, but the bound property in
my ViewModel returns a null on this.
Next, I am trying to pass the Row on the XamDataGrid as the item to my ContextMenu (Command)
handler in my ViewModel which relates to my effort on the previous point. I read numerous posts
about ContextMenu being not on the same Visual tree as the XamDataGrid itself, which is why the
property SelectedItems cannot be propogated to the Command. I even read that the SelectedItems
property on XamDataGrid is not aDependencyProperty which is why even upon binding it to a property
defined in the ViewModel, it wouldn't stick to it.
My goal is to pass the row values or the entire row of the XamDataGrid, when I right click on ContextMenu
to perform some action which needs these details in the CommandHandler in the ViewModel. How
can I achieve this?
I posted the code and my question even on stackoverflow here
http://stackoverflow.com/questions/3003583/using-mvvm-how-to-pass-selecteditems-of-a-xamdatagrid-as-parameter-to-the-comman
. Any help is very much appreciated.
HI,
If you don’t want to use Expressions here are some alternate suggestions from our developers.
The Triggers/Behaviors in the Expression assembly are just an encapsulated way to provide attached behaviors plus it has tooling support in Blend. If you don’t want to use those classes then you will need to write your own infrastructure or use someone else’s. Based on what Alex was doing on the forum thread, you would need to write something that can hook an event and invoke an action/command.
Searching the internet returned several approaches you might want to investigate:
· Blend EventTrigger: http://msdn.microsoft.com/en-us/library/ff726539%28v=Expression.40%29.aspx
· EventToCommand: http://www.galasoft.ch/mvvm/
· http://blogs.microsoft.co.il/blogs/tomershamam/archive/2009/04/14/wpf-commands-everywhere.aspx
· http://marlongrech.wordpress.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/
You will need to write a custom command or something to do the work of activating the record similar to what Alex did.
Another alternative is to write your own simple attached behavior. Since really all Alex was trying to do was to activate a record when the right mouse button was pressed one can write an attached property that hooks the previewmouserightbuttondown and activates the record.
Attached is a simple example demonstrating their last suggestion of adding an attached property that uses the previewmousrightbuttondown and activates the record.
Hello Shravan,
I am attaching this sample with a VS 2008 solution file. In the future please note that you will be able to open this project by opening the project file (.proj) with VS2008 and not the solution file (.sln).
Could this sample be opened using VS.NET 2008? Seems like this is a 2010 version, Could you respond with a 2008 version of files?
Thanks,
Shravan
Hello,
I have put together a small sample regarding these issues following the MVVM pattern.
For activating/selecting the record on mouse right click, it is best to use MicrosoftExpression.Interactions library and create a custom TargetedTriggerAction for the xamDataGrid, like this :
public class RecordActivationAction : System.Windows.Interactivity.TargetedTriggerAction<XamDataGrid>
{
protected override void Invoke(object parameter)
MouseButtonEventArgs args = parameter as MouseButtonEventArgs;
DataRecordPresenter drp = Infragistics.Windows.Utilities.GetAncestorFromType(args.OriginalSource as DependencyObject, typeof(DataRecordPresenter), true) as DataRecordPresenter;
if (drp != null)
Dispatcher.BeginInvoke(new Action(
() =>
drp.DataPresenter.ActiveRecord = drp.Record;
}), System.Windows.Threading.DispatcherPriority.ApplicationIdle, null);
}
and apply that action to the XamDataGrid :
<igDP:XamDataGrid ...>
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseRightButtonDown">
<local:RecordActivationAction />
</i:EventTrigger>
</i:Interaction.Triggers>
</igDP:XamDataGrid>
Regarding the ContextMenu commands, it depends on your MVVM implementation. In the attached sample I am using Josh Smith's commanding. The code for this is quite large so you can see this in the sample directly.
Hope this helps.