DataGrid reference from DataGridCell

Sep 24, 2008 at 8:04 PM
Hi,

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

Thank you!
Jon
Coordinator
Sep 24, 2008 at 10: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 8:31 PM
Thanks, that worked great!