private static DataGrid GetParentDatagrid(UIElement element)
        {
            UIElement childElement = null; // element from which to start the tree navigation, looking for a Datagrid parent

            if (element is ComboBoxItem)
            {
                // Since ComboBoxItem.Parent is null, we must pass through ItemsPresenter in order to get the parent ComboBox
                var parentItemsPresenter = VisualTreeFinder.FindParentControl <ItemsPresenter>(element as ComboBoxItem);
                var combobox             = parentItemsPresenter.TemplatedParent as ComboBox;
                childElement = combobox;
            }
            //else if (element is FrameworkElement)
            //{
            //    var fe = element as FrameworkElement;
            //    if (fe.Parent==null)
            //    {
            //        var ip = VisualTreeFinder.FindParentControl<ItemsPresenter>(fe);
            //        if (ip != null)
            //        {
            //            childElement = ip.TemplatedParent as UIElement;
            //        }
            //    }
            //}
            else
            {
                childElement = element;
            }

            var parentDatagrid = VisualTreeFinder.FindParentControl <DataGrid>(childElement);

            return(parentDatagrid);
        }
        private static void OnCommitOnLostFocusChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
        {
            var dataGrid = depObj as DataGrid;

            if (dataGrid == null)
            {
                return;
            }

            if (e.NewValue is bool == false)
            {
                return;
            }

            var   parentTabControl = VisualTreeFinder.FindParentControl <TabControl>(dataGrid);
            Panel tabPanel         = null;

            if (parentTabControl != null)
            {
                tabPanel = GetTabPanel(parentTabControl);
                if (tabPanel != null)
                {
                    ControlMap[tabPanel] = dataGrid;
                }
            }

            if ((bool)e.NewValue)
            {
                // Attach event handlers
                if (parentTabControl != null)
                {
                    tabPanel.PreviewMouseLeftButtonDown += OnParentTabControlPreviewMouseLeftButtonDown;
                }

                //dataGrid.LostKeyboardFocus += OnDataGridLostFocus;
                dataGrid.DataContextChanged += OnDataGridDataContextChanged;
                dataGrid.IsVisibleChanged   += OnDataGridIsVisibleChanged;
            }
            else
            {
                // Detach event handlers
                if (parentTabControl != null)
                {
                    tabPanel.PreviewMouseLeftButtonDown -= OnParentTabControlPreviewMouseLeftButtonDown;
                }

                //dataGrid.LostKeyboardFocus -= OnDataGridLostFocus;
                dataGrid.DataContextChanged -= OnDataGridDataContextChanged;
                dataGrid.IsVisibleChanged   -= OnDataGridIsVisibleChanged;
            }
        }