Selecting the current row during the PreviewMouseDown event
Basically, what it says on the title
I am looking to open a ContextMenu on the SfDataGrid during a long press, selecting the item/row as I do.
However, there seems to be no built-in way to access the row object during the event
I have tried traversing the VisualTree, but the Item is not a part of it.
Hi Bill,
|
Query |
Response |
|
|
I am looking to open a ContextMenu on the SfDataGrid during a long press, selecting the item/row as I do.
|
Based on the information provided, we understand that you need to open the ContextMenu during a long press and selecting the item. We are currently analyzing the scenario and need some time to validate the issue. We will provide further details by April 2, 2025. We appreciate your patience in the meantime. |
|
|
However, there seems to be no built-in way to access the row object during the event I have tried traversing the VisualTree, but the Item is not a part of it.
|
Based on the information provided, we understand that you need to retrieve data during the `PreviewMouseDown` event. This can be achieved by obtaining the row and column index based on the pointer position using the `PointToCellRowColumnIndex()` method and then retrieving the data using these indices.
Please refer to
the code snippet below: Code
Snippet:
|
Regards,
Malini Selvarasu
Hi Bill,
Based on the information provided, we understand that you need to open the ContextMenu on a long press while selecting a row. To enable row selection, set AllowSelectionOnPointerPressed to true, ensuring the row is selected when pressed. To display the ContextMenu, handle the PreviewMouseDown event with a time delay.
private void SfDataGrid_PreviewMouseDown(object sender, MouseButtonEventArgs e) { RowType rowType; var visualcontainer = this.dataGrid.GetVisualContainer(); // Gets the exact position where the mouse pointer is moved var point = e.GetPosition(visualcontainer); var rowColumnIndex = visualcontainer.PointToCellRowColumnIndex(point); rowType = visualcontainer.RowsGenerator.Items .FirstOrDefault(item => item.Index == rowColumnIndex.RowIndex)?.RowType ?? RowType.DefaultRow; if (rowType==RowType.DefaultRow && e.ChangedButton == MouseButton.Right) // Detect Right-Click Long Press { // Start long-press timer _longPressTimer = new DispatcherTimer(); _longPressTimer.Interval = TimeSpan.FromSeconds(1); // Adjust duration as needed _longPressTimer.Tick += LongPressTimer_Tick; _longPressTimer.Start(); } } // Called when long press is detected private void LongPressTimer_Tick(object sender, EventArgs e) { // Check if the button is still pressed if (Mouse.RightButton == MouseButtonState.Pressed) { ShowContextMenu(); } // Unsubscribe and stop the timer if (sender is DispatcherTimer timer) { timer.Tick -= LongPressTimer_Tick;// Unhook event timer.Stop(); // Stop the timer } } // Method to display ContextMenu private void ShowContextMenu() { dataGrid.RecordContextMenu.IsOpen = true; } |
Attachment: SfDataGrid_Demo_3338a2a6.zip