///// <summary>
        ///// Sets the Columns property.  This dependency property
        ///// indicates whether to use templates for the specified column.
        ///// </summary>
        //public static void SetColumns(DependencyObject d, AutoGenerateColumnCollection value)
        //{
        //    d.SetValue(ColumnsProperty, value);
        //}

        private static void OnColumnsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
            {
                AutoGenerateColumnCollection coll = e.NewValue as AutoGenerateColumnCollection;
                //coll.CollectionChanged += AutoGenerateColumn_CollectionChanged;

                DataGrid grid = coll.Owner as DataGrid;
                if (grid != null)
                {
                    grid.AutoGeneratingColumn += _grid_AutoGeneratingColumn;
                }
            }

            if (e.OldValue != null)
            {
                AutoGenerateColumnCollection coll = e.OldValue as AutoGenerateColumnCollection;
                //coll.CollectionChanged -= AutoGenerateColumn_CollectionChanged;

                DataGrid grid = coll.Owner as DataGrid;
                if (grid != null)
                {
                    grid.AutoGeneratingColumn -= _grid_AutoGeneratingColumn;
                }
            }
        }
        private static AutoGenerateColumnCollection GetColumnsInternal(DependencyObject d)
        {
            var collection = (AutoGenerateColumnCollection)d.GetValue(ColumnsProperty);

            if (collection == null)
            {
                collection = new AutoGenerateColumnCollection(d);
                d.SetValue(ColumnsProperty, collection);
            }
            return(collection);
        }
        public static void _grid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            DataGrid grid = sender as DataGrid;

            if (grid == null)
            {
                return;
            }

            AutoGenerateColumnCollection coll = GetColumns(grid);

            foreach (AutoGenerateColumn col in coll)
            {
                if (e.PropertyName == col.Column)
                {
                    CustomDataGridTemplateColumn templateColumn = new CustomDataGridTemplateColumn();
                    templateColumn.Header = e.Column.Header;
                    if (col.CellTemplate != null)
                    {
                        templateColumn.CellTemplate = col.CellTemplate;
                    }
                    if (col.CellEditingTemplate != null)
                    {
                        templateColumn.CellEditingTemplate = col.CellEditingTemplate;
                    }
                    if (col.Binding != null)
                    {
                        templateColumn.Binding = col.Binding;
                    }
                    templateColumn.SortMemberPath = e.Column.SortMemberPath;

                    e.Column = templateColumn;
                    return;
                }
            }

            if (e.Column.GetType() == typeof(DataGridTextColumn))
            {
                // Set the edit style to a style containing a custom context menu in order to handle ContextMenuOpening
                ((DataGridTextColumn)e.Column).EditingElementStyle = (Style)Application.Current.Resources["TextColumnEditStyle"];
                return;
            }

            return;
        }