Log in to like this post! ContextMenus, ToolTips, Mouse Clicks & Utilities John Doe / Wednesday, July 22, 2009 This is going to be a sample about Context Menus, ToolTips on different parts of the XamDataGrid. Creating a different ContextMenu or ToolTip for any part of the XamDataGrid in code behind is more flexible that a xaml defined one, despite the fact that the WPF Binding Engine is quite capable itself. I. The first step is to register/hook up the events to the elements that we are interested in (CellValuePresenter, DataRecordPresenter, LabelPresenter, etc). You can do this in two ways: 1. In Xaml using Styles and EventSetters: <Style TargetType="{x:Type igDP:DataRecordPresenter}"> <EventSetter Event="ToolTipOpening" Handler="DRPToolTip"/> </Style> 2.Using EventManager class and its RegisterClassHandler(...) method: EventManager.RegisterClassHandler(typeof(DataRecordPresenter), DataRecordPresenter.ContextMenuOpeningEvent, new ContextMenuEventHandler(DRPContext)); Once done this, you have all the instances of that type hooked up to these events and all you have to do is just to handle them appropriately with your custom logic. HowTo: Select Record using Right Mouse Button Down: Use 1. or 2. from the first step to register the tunneling event PreviewMouseRightButtonDown for the DataRecordPresenter. When handling this event, the sender will be your DataRecordPresenter, which exposes the Record property like this: void DRP_MouseRightButtonDown(object sender, MouseButtonEventArgs e) { DataRecordPresenter drp = sender as DataRecordPresenter; if (drp != null) drp.IsSelected = true; } HowTo: Use Utilities Helper methods: Last but not least are our helper methods in the Utilities class, which will help you go up/down the element tree searching for elements. 1. GetAncestorFromType - goes up the element tree searching for an element of a particular type. 2. GetAncestorFromName - goes up the element tree searching for an element by name. 3. GetDescendantFromType - goes down the element tree searching for an element of a particular type. 4. GetDescendantFromName - goes down the element tree searching for an element by name. They have couple of handy overloads, depending on what and how you are searching. Here is a code snippet demonstrating how to get the CellValuePresenter in the MouseMove event of the XamDataGrid: CellValuePresenter cvp = Infragistics.Windows.Utilities.GetAncestorFromType ( e.OriginalSource as DependencyObject, typeof(CellValuePresenter), false ) as CellValuePresenter; You can find the full sample in the attachments of this blog post. XamDataGrid_ContextMenus.zip