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 ?
So I wanted a context menu that allowed me to delete all of the selected rows, or clone just the row that was right-clicked on. This is what I did to get the right-click selection to work as expected. In my XAML:
---------------------------------
<!-- Define context menu --> <ContextMenu x:Key="RecordContextMenu"> <MenuItem Header="Delete Selected Rows" Click="grdConnectionManagerEntries_ContextMenuItem_Delete_Click" /> <MenuItem Header="Clone Selected Row" Click="grdConnectionManagerEntries_ContextMenuItem_Clone_Click" /> </ContextMenu> <Style TargetType="{x:Type igDP:DataRecordCellArea}"> <Setter Property="ContextMenu" Value="{DynamicResource RecordContextMenu}"></Setter> </Style> <!-- Make right-click select the clicked row --> <Style TargetType="{x:Type igDP:DataRecordPresenter}"> <EventSetter Event="PreviewMouseRightButtonUp" Handler="grdConnectionManagerEntries_DataRowPresenterRightClicked" /> </Style> </igDP:XamDataGrid.Resources>
And then in my code-behind I have this:
void grdConnectionManagerEntries_DataRowPresenterRightClicked(object sender, MouseButtonEventArgs e) { DataRecordPresenter drp = sender as DataRecordPresenter; if (drp != null && drp.Record != null) { // If we right-clicked on a row that was already selected, then maintain all of the currently selected rows, // otherwise clear any other selected rows so that just this one row gets selected. if (!drp.Record.IsSelected) grdConnectionManagerEntries.SelectedItems.Records.Clear(); // Select this row and set it as the active row as well. drp.Record.IsSelected = true; grdConnectionManagerEntries.ActiveRecord = drp.Record; } } private void grdConnectionManagerEntries_ContextMenuItem_Delete_Click(object sender, RoutedEventArgs e) { DataPresenterCommands.DeleteSelectedDataRecords.Execute(null, grdConnectionManagerEntries); } private void grdConnectionManagerEntries_ContextMenuItem_Clone_Click(object sender, RoutedEventArgs e) { CreateAndShowNewConnecitonEntryWindow((grdConnectionManagerEntries.ActiveDataItem as DataRowView).Row as Connection); }
I should point out though that I'm only dealing with Records in my XamDataGrid; no other types. I hope this helps.
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.
regards
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