简体   繁体   English

使用MVVM将行附加到WPF中的Datagrid

[英]Append a Row to a Datagrid in WPF using MVVM

I have a DataGrid in my View as shown below., 我的视图中有一个DataGrid,如下所示。

在此处输入图片说明

My Question is how can I Append the values from the textboxes to the row datagrid 我的问题是如何将文本框中的值附加到行datagrid

I have make sure that the Model has All the properties, When I click on the Add button it overwrites the dataGrid and shows only one latest record the and my ViewModel look like this: 我确保模型具有所有属性,当我单击“添加”按钮时,它会覆盖dataGrid并仅显示一个最新记录,而我的ViewModel如下所示:

class BatchItemsViewModel : ViewModelBase
    {

        public SearchItemsModel msearchItems { get; set; }

        ObservableCollection<SearchItemsModel> _BatchItemsGrid;

        public ObservableCollection<SearchItemsModel> BatchItemsGrid
        {
            get { return _BatchItemsGrid; }
            set
            {
                _BatchItemsGrid = value;
                OnPropertyChanged("BatchItemsGrid");
            }
        }

        private ICommand _addDataToBatchGrid;
        public ICommand addDataToBatchGrid
        {
            get
            {
                return _addDataToBatchGrid;
            }
            set
            {
                _addDataToBatchGrid = value;
            }
        }

        public BatchItemsViewModel()
        {
            msearchItems = new SearchItemsModel();
            addDataToBatchGrid = new RelayCommand(new Action<object>(AddDataInBatchGrid));

        }


        public void AddDataInBatchGrid(object obj)
        {
            ObservableCollection<SearchItemsModel> batchGridData = new ObservableCollection<SearchItemsModel>();
            var data = new SearchItemsModel
                            {
                                BatchNumber = msearchItems.BatchNumber,
                                MFDDate = msearchItems.MFDDate,
                                ExpiryDate = msearchItems.ExpiryDate,
                                Quantity = msearchItems.Quantity,
                            };
            batchGridData.Add(data);

            BatchItemsGrid = batchGridData; // HERE I am overwriting the datagrid

            //How can I Append the batchGridData to BatchItemsGrid (BatchItemsGrid.Append(batchGridData)???)
        }

    }

NOTE: I have gone through the other threads as well in the community for the similar posts but I couldn't find the appropriate and please correct me if I am going in wrong direction. 注意:我也浏览了社区中类似帖子的其他主题,但是我找不到合适的帖子,如果我走错了方向,请纠正我。

public void AddDataInBatchGrid(object obj)
        {
            var data = new SearchItemsModel
                            {
                                BatchNumber = msearchItems.BatchNumber,
                                MFDDate = msearchItems.MFDDate,
                                ExpiryDate = msearchItems.ExpiryDate,
                                Quantity = msearchItems.Quantity,
                            };
            this.BatchItemsGrid.Add(data);           
        }

...Should do the trick. ...应该做到这一点。 (don't replace the whole collection, just add items to it and let the notification events handle the UI updates) (不要替换整个集合,只需向其中添加项目,并让通知事件处理UI更新)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM