There is an interface 1:

public interface IService { string TextString {get;set;} } 

There is a class 1 implementing this interface 1:

 public class TextClass : IService, INotifyPropertyChanged { private string _textString ; public string TextString { get => _textString; set { _textString = value; OnPropertyChanged(); } } public TextClass { _textString = "XXXXX";} // Классическая реализация INotifyPropertyChanged // ..... } 

There is also an interface 2:

 public interface IDataService<T> { T : TextClassLocal } 

There is a class 2 implementing interface 2:

 public class DataService :IDataService<TextClass> { private TextClass _textClassLocal = new TextClass() public TextClass : TextClassLocal { get => _textClassLocal; set { _textClassLocal = value } } } 

There is a ViewModel:

 public class MainViewModel : INotifyPropertyChanged { private _dataServise = new DataService(); private string _textMessage public string TextMessage { get => _dataService.TextClassLocal.TextString; set { _dataService.TextClassLocal.TextString = value; OnPropertyChanged(); } } // Классическая реализация INotifyPropertyChanged // ..... } 

Ust View in which:

 <TextBlock Text={Binding TextMessage} /> 

The View DataContext is bound and the correct "XXXX" is displayed in the TextBlock at startup.

And now the most interesting, in any case for me;)

If the ViewModel to do the operation: TextString = "UUUU"; then the Text in the TextBlock will change to UUUU accordingly. All OK!

However, if done directly: _dataService.TextClassLocal.TextString = "UUUU"; then no changes will happen; (What could be the problem? How to get rid of it? In the combat implementation, the TextClass implements an Event that updates the TextString and these changes do not appear in TextBlock

    2 answers 2

    So in your TextString or TextMessage? In the code, one is in the description of the other ... If TextMessage, then your TextBlock listens to this property (ie, TM), upon changing which the PropertyChanged event is triggered. And so you change the private field that is not bugged by anyone. Therefore, no result

      In order for the user interface to learn about changes in the attached properties, WPF provides the INotifyPropertyChanged interface, which by calling PropertyChanged informs the client that the property value has changed.

      In the first version

       public string TextMessage { get => _dataService.TextClassLocal.TextString; set { _dataService.TextClassLocal.TextString = value; OnPropertyChanged(); } } 

      when you directly change the value of the TextMessage property of calling the OnPropertyChanged() method in the property setter, this is what causes the PropertyChanged event that causes the user interface to be notified and you can see the result.

      In the case of a call

       _dataService.TextClassLocal.TextString = "новое значение"; 

      notifications that the value has changed does not occur, so the user interface does not know anything about the change.

      In order for the user interface to update the value of the _dataService.TextClassLocal.TextString property after you directly change the _dataService.TextClassLocal.TextString value, _dataService.TextClassLocal.TextString can call the OnPropertyChanged() method and pass the name of the property whose value you want to update as a parameter.

       _dataService.TextClassLocal.TextString = "новое значение"; OnPropertyChanged("TextMessage");