Wpf datagrid contextmenu and SelectedCells - wpf

In a WPF DataGrid I use ContextMenuOpening(object sender, ContextMenuEventArgs e)
when the user right clicks on a cell.
I perform an action on the SelectedCells of the DataGrid which supports multi-selection.
But When I select a cell A and then perform the right click on a cell B two rows higher
the context menu is shown on cell B but the action is performed on cell A.
How would you solve this problem?

Related

WPF DataGrid loses focus after row delete / cut

I have a DataGrid implemented in an MVVM/Prism application. The DataGrid supports Cut/Copy/Paste/Delete through a context menu and keyboard gestures.
I find that when a row is deleted/cut the entire DataGrid loses focus and keyboard focus moves to the last focused control.
Is there anyway to prevent this?
After removing a row I may want to re-paste into the DataGrid. Furthermore if the grid is empty there is no way at all for it to get keyboard focus. Clicking an empty grid does not give it focus.
Here is a similar question, but it doesn't solve the issue for me:
DataGrid Looses Focus When Delete Key is Pressed
You could set the DataGrids Focus in the PreviewKeyDown-Event
private void TheDataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete)
{
var grid = (DataGrid)sender;
FocusManager.SetFocusedElement(Window.GetWindow(grid), grid); //not tested
}
}
If you dont want to put code in code-behind use AttachedProperties in combination with the DependencyPropertyChanged-Event.
How to set set focus.
A dirty solution might be to handle the LostFocus event exposed by DataGrid and set focus on the control.
It arguably marginally infringes on the MVVM pattern i.e keeping the view dumb as hell, but it's still view code.

How can i get the clicked button's row values on silverlight telerik radgridview button column?

I have a hyperlink button colum in silverlight telerik radgridview, as a cell template.
I am using MVVM.
When i click on the hyperlink button, how can i get its row's values(atleast in the code behind)..?
Thanks in advance..
You need to use the ParentOfType extension method to get hold of the row containing the button.
Once you have the row you can cast the item type to your viewmodel and access the properties of it.
Make sure you have a using statement for the extension method:
using Telerik.Windows.Controls;
private void Button_Click(object sender, RoutedEventArgs e)
{
var parent = (sender as Button).ParentOfType<GridViewRow>();
var rowVM = parent.Item as YourVmClass;
}
If you are using MVVM you can also use the EventToCommandBehavior.
This allows you to execute all events as commands. You can then handle the command in your viewmodel.

DataGrid + ContextMenu: How to get the row under the right click

In Silverlight 5, I have a DataGrid with a ContextMenuService.ContextMenu. If you click a row to select, then right click, you just check the grid.selecteditem for context. However, if you right click a row without selecting it, you don't have that row's context when the menu opens. How do you get the DataContext row of the grid that was right clicked on when the context menu opens? The right click on the grid seems to be an option, but it is intercepted for the contextmenu and does not fire unless a contextmenu is already open/in focus.
I have found tons of examples of getting around the original issue with Silverlight 4 and detecting the rown on right click. However, the contextmenu now intercepts the rigth click of the grid, so those no logner work. I also found posts on 'bugs' with the initial relase of the ContextMenu. All these posts/blogs are making it hard to find a current answer or solution.
yeah it seems like Silverlight 5 has changed something that breaks the old tricks.
We've been doing this: add row enters on row load handler. EG:
private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.MouseEnter += new MouseEventHandler(Row_MouseEnter);
e.Row.MouseLeave += new MouseEventHandler(Row_MouseLeave);
}
void Row_MouseEnter(object sender, MouseEventArgs e)
{
DataGridRow dgr = sender as DataGridRow;
IncidentGrid.SelectedItem = dgr.DataContext;
}
pretty ugly i know, but it's working.
Besides Roger's concerns of accuracy, I didn't want to wire up those events on every row because my grid is quite large, and I have to keep it lean.
I found a solution here...
https://mutelight.org/silverlight-datagrid-make-right-click-select-a-row
Apply just one event handler to the grid's MouseRightButtonDown event. The event args has the mouse position and you can use VisualTreeHelper to find which DataGridRow the mouse is over. Then that row's DataContext will have your SelectedItem.

How to get the item at the mouse position when I perform a Double Click on DataGrid in WPF?

I am new in WPF and I have a question about the DataGrid, how could I get the item at the mouse position when I perform a Double Click on DataGrid?
You can subscribe to the DoubleClick event using a Style and an EventSetter, for rows use the RowStyle, for specific cells the CellStyle.
In the handler you can then cast the sender to DataGridRow or DataGridCell respectively.
([Example with rows])

delete in multiple datagrids

I have three datagrids: MasterDatagrid, DetailDatagrid, AssocationDatagrid.
Bascially, if i select one row in one of the grid, i press 'delete' key to remove a row from that grid.
private void MasterDataGrid_IsMouseCapturedChanged(object sender, DependencyPropertyChangedEventArgs e)
{
??
}
how can i replicate this function in my delete button ? how can i detect where my mouse focus is? and how to detect the right datagrid i am in?
Many thanks
One way to do it:
You can define a focus scope in the xaml on your page or user control using FocusManager.IsFocusScope.
Then on the OnClicked event of your button you can get the focused element using FocusManager.GetFocusedElement() method.
From there you can get the required item using SelectedItem(s) property and delete it.

Resources