private void OnAttachToDataGridRow(DataGridRow dgr)
        {
            int index = dgr.GetIndex() + 1;

            if (AssociatedObject != null)
            {
                AssociatedObject.Text = index.ToString();
            }

            // Hook into the DataGridRow
            BindingOperations.SetBinding(this, TargetProperty, new Binding("Item")
            {
                Source = dgr
            });

            // Hook into the DataGrid owner.
            DataGrid dg = dgr.GetParent <DataGrid>();

            dg.RowEditEnding += DgRowEditEnding;
            var cv = CollectionViewSource.GetDefaultView(dg.ItemsSource);

            if (cv != null)
            {
                cv.CollectionChanged += CvCollectionChanged;
            }
        }
        /// <summary>
        /// Called when the behavior is being detached from its AssociatedObject, but before it has actually occurred.
        /// </summary>
        protected override void OnDetaching()
        {
            if (this.DataGridRow != null)
            {
                DataGrid dg = DataGridRow.GetParent<DataGrid>();
                var cv = CollectionViewSource.GetDefaultView(dg.ItemsSource);
                if (cv != null)
                    cv.CollectionChanged -= CvCollectionChanged;
                dg.RowEditEnding -= DgRowEditEnding;
            }

            base.OnDetaching();
        }