public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            PART_Columns = TryGetTemplateChild <Grid>("PART_Columns");

            if (PART_Columns != null)
            {
                foreach (var def in Owner.ColumnDefinitions)
                {
                    PART_Columns.ColumnDefinitions.Add(new ColumnDefinition()
                    {
                        SharedSizeGroup = "col_" + def.GetHashCode().ToString()
                    });
                    var template = def.ItemTemplateSelector == null ? def.ItemTemplate : def.ItemTemplateSelector.SelectTemplate(DataContext, this);
                    if (template != null)
                    {
                        var cell = new TreeGridViewCell();
                        cell.Content = template.LoadContent() as FrameworkElement;
                        cell.Style   = def.CellStyle;
                        cell.SetBinding(TreeGridViewCell.VisibilityProperty, new Binding("Visibility")
                        {
                            Source = def
                        });
                        if (def.Binding != null)
                        {
                            BindingOperations.SetBinding(cell, FrameworkElement.DataContextProperty, ((TreeGridContentColumnDefinition)def).Binding);
                        }
                        PART_Columns.Children.Add(cell);
                        Grid.SetColumn(cell, PART_Columns.ColumnDefinitions.Count - 1);
                    }
                }
            }
        }
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            if (Template == null)
            {
                return;
            }

            var grid = GetTemplateChild("PART_Grid") as Grid;

            if (grid == null)
            {
                System.Diagnostics.Trace.TraceError("The template of TreeGridView {0} does not contain a Grid element with name \"PART_Grid\".", this.GetHashCode());
            }
            else
            {
                foreach (var def in ColumnDefinitions)
                {
                    grid.ColumnDefinitions.Add(new ColumnDefinition()
                    {
                        SharedSizeGroup = "col_" + def.GetHashCode().ToString()
                    });
                    var header = new TreeGridViewCell();
                    header.SetBinding(TreeGridViewCell.ContentProperty, new Binding("Header")
                    {
                        Source = def
                    });
                    header.SetBinding(TreeGridViewCell.VisibilityProperty, new Binding("Visibility")
                    {
                        Source = def
                    });
                    grid.Children.Add(header);
                    Grid.SetColumn(header, grid.ColumnDefinitions.Count - 1);
                }
            }
        }