简体   繁体   English

在 Wpf MVVM 项目中上传 dataGrid

[英]Upload dataGrid in Wpf MVVM project

Here is the class of ChildViewModel:这是 ChildViewModel 的类:

public class ChildViewModel : Screen

    {

        private string imie = string.Empty;
        private string nazwisko = string.Empty;
        private string wiek = string.Empty;
        private Person person;
        private ObservableCollection<Person> personColl;
        private MainViewModel mainView = new MainViewModel();

        public ChildViewModel(Person person, ObservableCollection<Person> personColl)
        {
            this.person = person;
            this.personColl = personColl;
            this.Wyswietl();
        }

        public string ImieTxt
        {
            get => this.imie;

            set
            {
                this.imie = value;
                this.NotifyOfPropertyChange(() => this.ImieTxt);
            }
        }

        public string NazwiskoTxt
        {
            get => this.nazwisko;

            set
            {
                this.nazwisko = value;
                this.NotifyOfPropertyChange(() => this.NazwiskoTxt);
            }
        }

        public string WiekTxt
        {
            get => this.wiek;

            set
            {
                this.wiek = value;
                this.NotifyOfPropertyChange(() => this.WiekTxt);
            }
        }

        public void Zmien()
        {
            this.personColl[mainView.DataGridIndex].Imie = this.ImieTxt;
            this.personColl[mainView.DataGridIndex].Nazwisko = this.NazwiskoTxt;
            this.personColl[mainView.DataGridIndex].Wiek = this.WiekTxt;
            this.TryClose();
        }

        private void Wyswietl()
        {
            this.ImieTxt = this.person.Imie;
            this.NazwiskoTxt = this.person.Nazwisko;
            this.WiekTxt = this.person.Wiek;
        }
    }

I have no idea how to upload new data from ChildView to dataGrid in MainView, after clicking button " Zmien ".单击按钮“ Zmien ”后,我不知道如何将新数据从 ChildView 上传到 MainView 中的 dataGrid。 In MainView I have dataGrid, where from MainViewModel I'm loading data from the list.在 MainView 我有 dataGrid,我从 MainViewModel 加载列表中的数据。 After clicking button " Zmien ", new data doesn't load in dataGrid.单击按钮“ Zmien ”后,新数据不会加载到dataGrid中。
Maybe you have any idea how to do it?也许你知道怎么做?

From my article on Codeproject Guide to WPF DataGrid Formatting Using Bindings :从我关于 Codeproject Guide to WPF DataGrid Formatting Using Bindings 的文章

Connecting a DataGrid with Business Data将 DataGrid 与业务数据连接起来

Even connecting a DataGrid with the business data is not trivial.甚至将 DataGrid 与业务数据连接起来也并非易事。 Basically, a CollectionViewSource is used to connect the DataGrid with the business data:基本上,一个 CollectionViewSource 用于将 DataGrid 与业务数据连接起来:

The CollectionViewSource does the actual data navigation, sorting, filtering, etc. CollectionViewSource 执行实际的数据导航、排序、过滤等。

<Window.Resources>
    <CollectionViewSource x:Key="ItemCollectionViewSource"  CollectionViewType="ListCollectionView"/>
</Window.Resources> 


<DataGrid
  DataContext="{StaticResource ItemCollectionViewSource}"
  ItemsSource="{Binding}"
  AutoGenerateColumns="False"
  CanUserAddRows="False">  


//create business data
var itemList = new List<stockitem>();
itemList.Add(new StockItem {Name= "Many items",      Quantity=100, IsObsolete=false});
itemList.Add(new StockItem {Name= "Enough items",    Quantity=10,  IsObsolete=false});
...

//link business data to CollectionViewSource
CollectionViewSource itemCollectionViewSource;
itemCollectionViewSource = (CollectionViewSource)(FindResource("ItemCollectionViewSource"));
itemCollectionViewSource.Source = itemList; 
  1. Define a CollectionViewSource in Windows.Resource在 Windows.Resource 中定义 CollectionViewSource
  2. The gotcha here is that you must set the CollectionViewType.这里的问题是您必须设置 CollectionViewType。 If you don't, the GridView will use BindingListCollectionView, which does not support sorting.如果不这样做,GridView 将使用不支持排序的 BindingListCollectionView。 Of course, MSDN does not explain this anywhere.当然,MSDN 并没有在任何地方解释这一点。
  3. Set the DataContext of the DataGrid to the CollectionViewSource.将 DataGrid 的 DataContext 设置为 CollectionViewSource。
  4. In the code behind, find the CollectionViewSource and assign your business data to the Source property在后面的代码中,找到 CollectionViewSource 并将您的业务数据分配给 Source 属性

In this article, data gets only read.在本文中,仅读取数据。 If the user should be able to edit the data, use an ObservableCollection.如果用户应该能够编辑数据,请使用 ObservableCollection。 However, it is often better to leave the DataGrid readonly, because editing in the DataGrid behaves differently from what one is used from spreadsheet programs.但是,让 DataGrid 保持只读状态通常更好,因为在 DataGrid 中编辑的行为与在电子表格程序中使用的行为不同。 It might be better if the user has to doubleclick on the row he wants to change and open another window just for editing that entity or adding a new one.如果用户必须双击他想要更改的行并打开另一个窗口来编辑该实体或添加新实体,这可能会更好。

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

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