简体   繁体   中英

Upload dataGrid in Wpf MVVM project

Here is the class of 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 ". In MainView I have dataGrid, where from MainViewModel I'm loading data from the list. After clicking button " Zmien ", new data doesn't load in dataGrid.
Maybe you have any idea how to do it?

From my article on Codeproject Guide to WPF DataGrid Formatting Using Bindings :

Connecting a DataGrid with Business Data

Even connecting a DataGrid with the business data is not trivial. Basically, a CollectionViewSource is used to connect the DataGrid with the business data:

The CollectionViewSource does the actual data navigation, sorting, filtering, etc.

<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
  2. The gotcha here is that you must set the CollectionViewType. If you don't, the GridView will use BindingListCollectionView, which does not support sorting. Of course, MSDN does not explain this anywhere.
  3. Set the DataContext of the DataGrid to the CollectionViewSource.
  4. In the code behind, find the CollectionViewSource and assign your business data to the Source property

In this article, data gets only read. If the user should be able to edit the data, use an 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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