Is it possible to get dynamic columns on wpf datagrid in mvvm pattern?

前端 未结 2 1640
逝去的感伤
逝去的感伤 2020-12-24 09:21

Im developing a product in wpf (mvvm pattern). I have a scenario in which i have spent more than a week for no good result. Pls help me if you can... Here is the scenario, a

2条回答
  •  半阙折子戏
    2020-12-24 09:38

    I would like to extend the previous example (answer) the ability to subscribe and unsubscribe on event CollectionChanged.

    Behavior (add reference on System.Windows.Interactivity):

     public class ColumnsBindingBehaviour : Behavior
    {
        public ObservableCollection Columns
        {
            get { return (ObservableCollection) base.GetValue(ColumnsProperty); }
            set { base.SetValue(ColumnsProperty, value); }
        }
    
        public static readonly DependencyProperty ColumnsProperty = DependencyProperty.Register("Columns",
            typeof(ObservableCollection), typeof(ColumnsBindingBehaviour),
                new PropertyMetadata(OnDataGridColumnsPropertyChanged));
    
        private static void OnDataGridColumnsPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            var context = source as ColumnsBindingBehaviour;
    
            var oldItems = e.OldValue as ObservableCollection;
    
            if (oldItems != null)
            {
                foreach (var one in oldItems)
                    context._datagridColumns.Remove(one);
    
                oldItems.CollectionChanged -= context.collectionChanged;
            }
    
            var newItems = e.NewValue as ObservableCollection;
    
            if (newItems != null)
            {
                foreach (var one in newItems)
                    context._datagridColumns.Add(one);
    
                newItems.CollectionChanged += context.collectionChanged;
            }
        }
    
        private ObservableCollection _datagridColumns;
    
        protected override void OnAttached()
        {
            base.OnAttached();
    
            this._datagridColumns = AssociatedObject.Columns;
        }
    
    
        private void collectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    if (e.NewItems != null)
                        foreach (DataGridColumn one in e.NewItems)
                            _datagridColumns.Add(one);
                    break;
    
                case NotifyCollectionChangedAction.Remove:
                    if (e.OldItems != null)
                        foreach (DataGridColumn one in e.OldItems)
                            _datagridColumns.Remove(one);
                    break;
    
                case NotifyCollectionChangedAction.Move:
                    _datagridColumns.Move(e.OldStartingIndex, e.NewStartingIndex);
                    break;
    
                case NotifyCollectionChangedAction.Reset:
                    _datagridColumns.Clear();
                    if (e.NewItems != null)
                        foreach (DataGridColumn one in e.NewItems)
                            _datagridColumns.Add(one);
                    break;
            }
        }
    }
    

    View:

    
    
        
            
                
            
            
                        
            
        
    
    

    ViewModel:

         private ObservableCollection _dataGridColumns;
         public ObservableCollection DataGridColumns
         {
             get
             {
                 if (_dataGridColumns == null)
                     _dataGridColumns = new ObservableCollection()
                     {
                         new DataGridTextColumn()
                         {
                             Header = "Column1"
                         }
                     };
    
                 return _dataGridColumns;
             }
             set
             {
                 _dataGridColumns = value;
                 OnPropertyChanged("DataGridColumns");
             }
         }
    

提交回复
热议问题