Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
50
Accessing XamGridRow from Context Menu
posted

Does anyone know how to access the data record (e.g. Business object) supplying a row in a XamDataGrid from a context menu on the grid?

Here's what I'm doing.

I am programatically adding a Context Menu to the Grid:

xamDataGrid1.ContextMenu = new ContextMenu();

MenuItem sellItem = new MenuItem();

sellItem.Header = "Sell this";sellItem.Click += new RoutedEventHandler(Item_Click);

xamDataGrid1.ContextMenu.Items.Add(sellItem);

 

Then in the click event, I have tried numerous ways to access the data in the current row, one of those ways being the same way that other developers in our shop have accessed the data behind a row in the double click event. (see below)

(this works just fine):

private void XamDataGrid1_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e){

DependencyObject source = e.OriginalSource as DependencyObject;

if (source != null){

   CellValuePresenter cvp = Infragistics.Windows.Utilities.GetAncestorFromType(source, typeof(CellValuePresenter), true  as CellValuePresenter;

   if (cvp != null){

       someObject = ((<businessObject>)(((DataItemPresenter)(cvp)).Record.DataItem)).ProductKey;

       ...

     }

  }

}

Anyone got any ideas? 

Thanks,

 Damian

Parents
No Data
Reply
  • 8576
    Offline posted
    Hi Damian -
     
    The code in your double-click event is calling GetAncestorFromType passing in e.OriginalSource as the first parameter, so the search for the ancestor (i.e., CellValuePresenter in this case) starts at that element.  When the element is in fact a descendant of a CellValuePresenter the ancestor walk works fine.
     
    However, if you are use the same code from within the MenuItem click, the GetAncestorFromType method will not find the CellValuePresenter because the element returned from e.OriginalSource is the MenuItem (or one of its descendants) and it is not a descendant of the CellValuePresenter.
     
    What you could do instead is access the grid's currently selected record (this.myGrid.SelectedItems.Records[0]) from inside the MenuItem click event, and then do what you need with the record (assuming its reasonable for your application to require that the user select a record before right-clicking on it)
     
    Joe
     
Children
No Data