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
385
How can I access the cell's visual elements in the CellActivated event?
posted

I am using a CellValuePresenter to place a Grid and child elements of the Grid into each cell. When I handle the CellActivated event of the xamDataGrid, how can I access the WPF Grid element in that cell?

I looked at the CellActivatedEventArgs but didn't see anything in it that looked promising.

  • 2426
    Verified Answer
    posted

    I would recommend getting the CellValuePresenter from the CellActivatedEventArgs and then using the VisualTreeHelper to find the child you want:

            void grid_CellActivated(object sender, CellActivatedEventArgs e)
            {
                var cvp = CellValuePresenter.FromCell(e.Cell);
                int count = VisualTreeHelper.GetChildrenCount(cvp);
                for (int i = 0; i < count; i++)
                {
                    var child = VisualTreeHelper.GetChild(cvp, i) as TextBlock;
                    if (child != null)
                        child.Text = "New Text";
                }
            }

    Just remember that the XamDataGrid uses record recycling by default and this may produce duplicate values while scrolling through the records.