DataGrid reference from DataGridCell

Sep 24, 2008 at 7:04 PM

Is it possible to get a reference to a DataGrid instance from a DataGridCell instance?  If so how?

Thank you!
Sep 24, 2008 at 9:41 PM
You can go up the visual tree to get the DataGrid.  Here is some example code for a generic helper to look for a parent of a UIElement:

public static T FindVisualParent<T>(UIElement element) where T : UIElement
            UIElement parent = element;
            while (parent != null)
                T correctlyTyped = parent as T;
                if (correctlyTyped != null)
                    return correctlyTyped;

                parent = VisualTreeHelper.GetParent(parent) as UIElement;

            return null;

Oct 8, 2008 at 7:31 PM
Thanks, that worked great!