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 ?
Any ideas?
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,
Hello,
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.
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
perfect - so in summary, one would register for the DataRecordPresenter's PreviewMouseRightButtonDownEvent and force the record to be selected
EventManager.RegisterClassHandler(typeof(DataRecordPresenter), DataRecordPresenter.PreviewMouseRightButtonDownEvent, new MouseButtonEventHandler(DRP_MouseRightButtonDown));
void DRP_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
DataRecordPresenter drp = sender as DataRecordPresenter;
if ( drp != null ) drp.IsSelected = true;
}