【问题标题】:Two way binding to a Datagrid with a list in a new window在新窗口中使用列表两种方式绑定到 Datagrid
【发布时间】:2015-11-09 23:50:07
【问题描述】:

我在将列表数据绑定到数据网格时遇到了一些问题。我的代码如下。

我想绑定我的 observable 集合;

        ObservableCollection<RectangleContour> rectContourList = new ObservableCollection<RectangleContour>();

矩形轮廓对象;

        public class RectangleContour : INotifyPropertyChanged
    {
        public int Property1{ get; set; }
        public int Property2{ get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        // This method is called by the Set accessor of each property. 
        // The CallerMemberName attribute that is applied to the optional propertyName 
        // parameter causes the property name of the caller to be substituted as an argument. 
        private void NotifyPropertyChanged( String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

我想将列表绑定到另一个窗口中存在的数据网格视图,该窗口从上述对象所属的窗口调用,我这样调用新窗口;

                Window window = new Window
            {
                Title = "My User Control Dialog",
                Content = new UserControl1(list1: rectContourList),
            };
            window.ShowDialog();
            window.Close();

数据正确显示,如果我编辑网格上已经存在的元组,则更改会像我想要的那样反映在我的 MainWindow 中,但是我也希望能够通过数据网格添加新对象,并且当我尝试这样做时,我在 window.close() 上得到一个异常,就像说我需要 Path 或 xPath 来实现 TwoWay Bind。

谁能指出我正确的方向,为什么会发生这种情况?我是否需要在两个窗口之间以及数据网格和可观察集合之间设置数据绑定?

感谢您的帮助。

【问题讨论】:

    标签: c# wpf winforms data-binding datagrid


    【解决方案1】:

    使用这个类而不是一个简单的列表可能会对你的情况有所帮助:

    public class ObservableListSource<T> : ObservableCollection<T>, IListSource 
            where T : class 
        { 
            private IBindingList _bindingList; 
    
            bool IListSource.ContainsListCollection { get { return false; } } 
    
            IList IListSource.GetList() 
            { 
                return _bindingList ?? (_bindingList = this.ToBindingList()); 
            } 
        } 
    

    编辑:

    如果您想跟踪模型的变化,例如在断开模式下,以下类非常方便:

      public class EditableList<TModel> : ObservableList<TModel>
            where TModel : class, INotifyPropertyChanged
        {
            #region Properties
    
            public List<TModel> EditedItems { get; set; }
            public List<TModel> AddedItems { get; set; }
            public List<TModel> DeletedItems { get; set; }
    
            public Boolean HasChanged
            {
                get
                {
                    return
                    (
                        this.EditedItems.Count > 0 ||
                        this.AddedItems.Count > 0 ||
                        this.DeletedItems.Count > 0
                    );
                }
    
            }
            #endregion
    
            #region Constructor
            public EditableList(IList<TModel> list)
                : base(list)
            {
                this.AddedItems = new List<TModel>();
                this.EditedItems = new List<TModel>();
                this.DeletedItems = new List<TModel>();
    
                this.CollectionChanged += EditableList_CollectionChanged;
    
                foreach (var item in list)
                    item.PropertyChanged += TModelEditableList_PropertyChanged;
            }
            public EditableList() : this(new List<TModel>()) { }
    
    
            #endregion
    
            #region Events
            void TModelEditableList_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
            {
                if (!(sender is TModel))
                    return;
    
                var entity = (TModel)sender;
    
                this.AddToEditedItems(entity);
    
            }
    
            void EditableList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
            {
                if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
                    foreach (TModel entity in e.NewItems)
                    {
                        this.AddToAddedItems(entity);
                    }
    
                else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
                    foreach (TModel entity in e.OldItems)
                    {
                        this.AddToDeletedItems(entity);
                    }
            }
    
            #endregion
    
            #region Methods
            public void AddToEditedItems(TModel entity)
            {
                if (!this.EditedItems.Contains(entity) && !this.AddedItems.Contains(entity))
                    this.EditedItems.Add(entity);
            }
            public void AddToAddedItems(TModel entity)
            {
                this.AddedItems.Add(entity);
                entity.PropertyChanged += TModelEditableList_PropertyChanged;
            }
            public void AddToDeletedItems(TModel entity)
            {
                if (this.EditedItems.Contains(entity))
                {
                    this.EditedItems.Remove(entity);
                    this.DeletedItems.Add(entity);
                }
                else if (this.AddedItems.Contains(entity))
                    this.AddedItems.Remove(entity);
                else
                    this.DeletedItems.Add(entity);
            }
            public void ClearEditHistory()
            {
                this.EditedItems.Clear();
                this.AddedItems.Clear();
                this.DeletedItems.Clear();
            }
            #endregion
        }
    

    我希望这会有所帮助。

    【讨论】:

    • 感谢您的回复,但我不完全理解这种可能的解决方案,您是否建议将 rectContourList 替换为您的头等舱?在这种情况下,二等舱如何提供帮助?您能否进一步解释此解决方案的方法,以便我了解如何更好地实施它?感谢您的宝贵时间。
    • @user4853750 当然是我的朋友。是的,第一类应该用于绑定您的数据网格。 (dataGridview1.DataSource=...)第二个类跟踪您在 DataGrid 中的更改并将它们保存在三个单独的列表中。这对于离线工作非常有用。我的意思是当您想保留更改并稍后保存时。
    猜你喜欢
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    • 2012-03-23
    相关资源
    最近更新 更多