internal void GenerateColumns()
        {
            var remove = _grid.Columns.Where(c => DynamicGrid.GetInfo(c) != null).ToList();

            remove.ForEach(c => _grid.Columns.Remove(c));
            AddColumns(_columns);
        }
        private void ColumnsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
            case NotifyCollectionChangedAction.Add:
                AddColumns(e.NewItems);
                break;

            case NotifyCollectionChangedAction.Remove:
                var remove = _grid.Columns.Where(column => e.OldItems.Contains(DynamicGrid.GetInfo(column))).ToList();
                remove.ForEach(c => _grid.Columns.Remove(c));
                break;

            case NotifyCollectionChangedAction.Replace:
                GenerateColumns();
                break;

            case NotifyCollectionChangedAction.Move:
                break;

            case NotifyCollectionChangedAction.Reset:
                GenerateColumns();
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        public DynamicGridColumn(DataGrid grid, IDynamicGridColumnInfo info)
        {
            _grid = grid;
            DynamicGrid.SetInfo(this, info);
            var notify = info as INotifyPropertyChanged;

            if (notify != null)
            {
                notify.PropertyChanged += InfoPropertyChanged;
            }
        }
        protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
        {
            if (dataItem == null || dataItem == CollectionView.NewItemPlaceholder)
            {
                return(base.GenerateElement(cell, dataItem));
            }

            //var items = dataItem as IEnumerable;
            //if (items == null)
            //    throw new Exception(
            //        "When using DynamicGrid, ItemsSource must implement IEnumerable " +
            //        "and its members must implement IEnumerable. For better performance " +
            //        "its members should also implement IList.");
            var manager = new CellManager(_grid, dataItem, Info);

            DynamicGrid.SetCellManager(cell, manager);
            cell.SetBinding(FrameworkElement.DataContextProperty, new Binding("Data")
            {
                Source = manager
            });
            return(base.GenerateElement(cell, manager.Data));
        }