I'm capturing MouseDown event in my UltraGrid and need to figure out when user clicks on a cell in a grid. I was trying to do the following
UIElement lastElementEntered = grid.DisplayLayout.UIElement.LastElementEntered; if (lastElementEntered == null) return;
CellUIElement cellUIElement = lastElementEntered as CellUIElement; if (cellUIElement != null) { <do some stuff here> }
but it never gets into the <do some stuff here> area because type of the lastElementEntered is EditorWithTextDisplayTextUIElement and not CellUIElement. Should I just check for EditorWithTextDisplayTextUIElement type or is there a better way?
If you want the cell, the easiest thing to do is just to do this:
UltraGridCell cell = lastElementEntered.GetContext(typeof(UltraGridCell)) as UltraGridCell;
This will return the cell (or null) regardless of the UIElement you are over.
If you want to understand why what you have here doesn't work, it's because the CellUIElement contains other elements and those element completely fill the cell element. I recommend that you try the Infragistics UIElementViewer Utility. It will give you a nice display of what element exist, their hierarchy, and positions.