0

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?

XAMlMAX
  • 2,268
  • 1
  • 14
  • 23
  • Hi! Maybe [this](https://stackoverflow.com/questions/13296399/mvvm-datagrid-binding) may help. In short, the collection should be in parent collection and contain list or data for DataGrid rows. This collection may be attached to `DataGrid` in xaml this way: ``. – aepot Feb 28 '20 at 19:27
  • Where is your xaml with how you bind to it? Also your child VM is creating MainVM!!!!! – XAMlMAX Mar 02 '20 at 12:37

1 Answers1

0

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.

Peter Huber
  • 3,052
  • 2
  • 30
  • 42