I have set a contextmenu on the grid and am changing the state of various items in that menu in response to the selection changing on the grid. All works as expected if i first left click on a row before right clicking on it (in that the context menu is correctly based on the currently selected row), however if i right click on an unselected row, selection doesn't change and as such i don't get a correct context menu.
How do i get the grid to change selection on right click ?
Hello,
You may find this blog post and its sample helpful.
http://blogs.infragistics.com/blogs/alex_fidanov/archive/2009/07/22/contextmenus-tooltips-mouse-clicks-amp-utilities.aspx
Slight problem with the solution suggested - the grid seems to be removing selection on other rows when i do the following
. Highlight multiple rows. right click on the last row ( perform the right click on all the rows on the selection)
On the right click, all previous rows are unselected. Why is this? Also, if the user right clicks a row, then moves to another row (without dismissing the context menu) then multiple rows are selected with each right click... very strange behaviour.
How do i keep the selections for the right click menu ?
regards
If you use this :
drp.IsSelected = true;
then the SelectedItems.Records collection will be cleared and this record will be added there. However, before setting this to selected, you can check what you have in that collection- XamDataGrid.SelectedItems.Records. Moreover, you can add the record in that collection rather than setting the IsSelected property.
I've tried this
SelectedItems.Records.Add( drp.Record );
and I get the following exception
"Records must be valid records from display layout of the same dataPresenter.
Parameter name: records"
cheers,
What is the ultimate goal that you want to achieve?
This isn't the issue i'm having -
If you run the attached project you'll see that the select row count is wrong when right clicking on additional rows - so with one row selected, right clicking on a different row will print out '1' to the project output when it should be 2.
I see where the problem is. The SelectionChanged event will fire for all the types of objects that can be selected in the XamdataGrid - Cells, Fields, Records. So, what you need to do is check the e.Type property and see which type is being selected:
if (e.Type == typeof(Record))
it looks like the SelectionChanged event is fired but the SelectedItems collection doesn't contain the correct count of rows.
I have attached a sample showing this
I'm an idiot.. sorry.
I didn't twig that
EventManager.RegisterClassHandler( typeof( DataRecordPresenter ), PreviewMouseRightButtonDownEvent,
new MouseButtonEventHandler( onMouseRightButtonDown ) );
is a global registration ....
silly me :)
thanks!