0
votes

I have a model class Person, which contains string property Name.

I have a repository class Persons which in its constructor creates a few instances of Person class and it has a method which returns all Persons.

I have a viewmodel class PersonsViewModel which is inherited from ViewModelBase that implements INotifyPropertyChanged.

PersonsViewModel have ObservableCollection property AllPersons that contains data from Persons.

In view, I have list of textboxes that are bound to Names.

ItemsControls is bound to Allpersons.

So, if I have two times list of textboxes in my view. They are copies, so they are bound to same properties.

If I edit a textbox in the first list and put the cursor away from the textbox, why does it automatically change the value in the textbox in the second list.

Please, can anybody explain me why does it happen.

I know that I need to use OnPropertyChanged when I edit ViewModels properties after commands, but I didn't use them in this example.

I am new in MVVM and WPF.

Thank you in advance :)

2
Please provide a code sample, it's hard to find the issue just from your explanations. - rth
I didn't actually have a problem with my code. In fact I needed explanation how binding works and how the view notifies the viewmodel about modification. - anttiu

2 Answers

0
votes

I don't know if any of us understands your problem here but ..

So, if I have two times list of textboxes in my view. They are copies, so they are bound to same properties.

If I edit a textbox in the first list and put the cursor away from the textbox, why does it automatically change the value in the textbox in the second list.

This happens because you have changed the same object which was bound to two different controls.

0
votes

This just sounds like exactly what should be happening.

You've bound the ItemsSource of the two ItemsControls to the same collection (AllPersons) of Person objects. You've bound your TextBoxes to the Name property of the Person object. When you input characters in a TextBox, you're changing its TextBox.Text property. That change is persisted to your ViewModel when the UpdatePropertyChanged event fires. TextBox.Text is set to UpdatePropertyChanged = LostFocus by default, so whenever you input characters into the TextBox and then click somewhere else the UpdatePropertyChanged event fires and updates the Name property of your Person in the AllPersons collection. The change to this property causes the binding to the second ItemsControl/TextBox to fire and its value updates.