Please note: In an effort to better serve you, we are in the process of restructuring DevCenter.
In the process, we have moved many items that you may be used to finding in DevCenter over to the
Main Site. If you are having trouble locating something,
please try looking at the following places:
There are several situations where a developer would want a specific action to be taken on a right-click on the WinGrid, depending on where the user clicked.This tutorial uses the MouseDown event of the grid, wherein the element clicked is determined and appropriate action taken, depending on which options are selected.
Question: How can I determine which element a user clicked on?Solution: It is necessary to get the UIElement from the grid based on the coordinates of the mouse cursor, and from here to check for the element in which you are interested, such as a Header.
This sample project allows the user to select from two actions to take for each of three elements: a cell, a header, and a row selector.To start, we must get the UIElement found at the mouse position, using the following code:In C#:
From this UIElement, we need to check to see if we can find the specific element that we are interested in. This is accomplished by the GetContext() and GetAncestor() methods of the UIElement itself, where we specify the UIElement type that we want to find. The following code shows how to check for a header, cell, or row selector, and then show a context menu, select the relevant object, or in the case of a row selector, delete the associated row:In C#:
Review:This project shows how to capture the UIElement where the user clicked and take a specific action depending on what kind of UIElement this object is.