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
410
How to determine clicked cell for selected Record
posted

Hi,

when the user clicks on a cell in my grid, the row, the field and two additional fields should be highlighted (always 3 rows are a logical unit that should be highlighted together). The grid is not editable.

I have set CellClickAction to SelectRecord in the FieldLayout FieldSettings. Now I handle the SelectedItemsChanged event to update other parts of the system which record is selected and to select the adjacent fields. Here is my problem: I can't figure out which cell has been clicked. SelectedItems only contains the selected Record, ActiveCell is not set.

If I set CellClickAction to SelectCell, SelectedItemsChanged is not fired. Am I missing something?

 

Thanks for your help

 

Bettina

 

 

 

  • 2677
    posted

    Hello Bettina,

    You are correct.  If you put the grid in RecordSelection mode, you will not get an active cell.  However, I do have a solution for you.  If you handle the PreviewMouseLeftButtonDown event, you can get the element that you clicked on and then find the CellValuePresenter that contains it.  YOu must use the preview event as IG will handle the MouseLeftButtonDown so the regular MouseLeftButtonDown event will not fire.  Once you find the CellValuePresenter, you can set the IsActive property from there.  Now, since all this is done before the selectedItems changed event handler, you now have access the the active cell and can make your selections and use the active cell for whatever you need it for.  Then, you can set the ActiveCell back to null if you need to so you dont see the visual effects of the active cell. 

    Here is the code to get the CellValuePresenter and set the active cell:

     private void XamGrid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                DependencyObject dobj = (DependencyObject)e.OriginalSource;
                while (dobj != null)
                {
                    if (dobj is CellValuePresenter)
                    {
                        ((CellValuePresenter)dobj).IsActive = true;
                        break;
                    }
                    dobj = VisualTreeHelper.GetParent(dobj);
                }
            }