This is a migrated thread and some comments may be shown as answers.

Setting up an EventToCommand with selected cell

9 Answers 382 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Alex
Top achievements
Rank 1
Alex asked on 31 Oct 2012, 03:08 PM
I'm converting an existing DataGrid to a RadGridView. On my DataGrid I have trigger set up to fire whenever I right click on a cell. It looks like this:

<i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseRightButtonDown">
                    <mvvmcmd:EventToCommand Command="{Binding ItemClickedCommand}" CommandParameter="{Binding CurrentCell, ElementName=ContractProxyDataGrid}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>

I'm trying to do a similar thing with my RadGridView but binding to CurrentCell gives me a null parameter. What I am trying to get at is the DataGridCellInfo (or something similar) of the cell that was right clicked. What should I be binding too?

Thanks.

9 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 01 Nov 2012, 10:03 AM
Hello Alex,

That would be the expected behavior since MouseRightButtonDown does not cause any cell to be set as current. What is the exact behavior that you want to get ? 

Regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Alex
Top achievements
Rank 1
answered on 01 Nov 2012, 12:29 PM
I'm trying to capture the cell that was right clicked and pass its info to the viewmodel.
0
Maya
Telerik team
answered on 01 Nov 2012, 01:06 PM
Hi Alex,

Generally, it is not recommended to work with visual elements in your ViewModel. Still, if you need to have the cell you right-clicked on, you can pass the arguments of the event to the command and work with them in the view model:

<cmd:EventToCommand PassEventArgsToCommand="True"  />

 

Kind regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Alex
Top achievements
Rank 1
answered on 01 Nov 2012, 02:20 PM
Unfortunately, PassEventArgsToCommand doesn't quite fit my needs as it returns MouseButtonEventArgs which don't have the info that I require.

Essentially what I'm trying to do is, when a cell is right-clicked I need to get the name of the column in which that cell is and what "row" or "item" it is a part of. Any ideas?

Thanks.
0
Maya
Telerik team
answered on 01 Nov 2012, 02:55 PM
Hi Alex,

You can get the the OriginalSource from the arguments and using ParentOfType<T>() extension method to find parent cell and parent row. Still, I will recommend you to consider your architecture so that you do not work with visual elements in your ViewModel.

Greetings,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Alex
Top achievements
Rank 1
answered on 01 Nov 2012, 03:51 PM
What is a better pattern to use in this case? To facilitate handling the right-click on a cell and then doing some sort of work?

Thanks
0
Maya
Telerik team
answered on 02 Nov 2012, 08:01 AM
Hi Alex,

My recommendation would be to handle directly the event and update the architecture so that you do not need to work with visual elements in your ViewModel. If you do not want to have code in the YourPage.xaml.cs file, you can try creating an attached behavior instead and handle the event there. 

Regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Alex
Top achievements
Rank 1
answered on 02 Nov 2012, 12:10 PM
Now matter where I handle the event, I will need to know the row and cell that was right-clicked. Should I do this as you suggested with OriginalSource from the arguments and using ParentOfType<T>() extension method to find parent cell and parent row, or is there a way for right-click to actually select a cell so I can then use something like CurrentCell or SelectedItem?
0
Maya
Telerik team
answered on 02 Nov 2012, 12:40 PM
Hi Alex,

You need to find the parent row and set it as selected.    

Greetings,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
GridView
Asked by
Alex
Top achievements
Rank 1
Answers by
Maya
Telerik team
Alex
Top achievements
Rank 1
Share this question
or