Began to study MVVM and faced with confusion. For example, to write data from a ViewModel to a View, you need to fill it in the ViewModel () constructor. And if I need to change the data in the stream and change it in the View. Tried to make the delegate broadcast in a separate stream, but the data does not change.

View

<Window x:Class="information_system_wpf.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" WindowStyle="None" WindowState="Normal" AllowsTransparency="True" Background="Transparent" Height="350" Width="525"> <Window.Resources> <Style TargetType="TextBlock" x:Key="RunningLine"> <Setter Property="Foreground" Value="Red" /> <Setter Property="FontSize" Value="30"/> <Style.Triggers> <EventTrigger RoutedEvent="TextBlock.Loaded"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetProperty="(Canvas.Right)" From="525" To="-150" Duration="0:0:01" RepeatBehavior="Forever"/> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> </Style.Triggers> </Style> </Window.Resources> <Grid Background="White" DataContext="{Binding Models}"> <Canvas > <TextBlock Text="{Binding RunLine, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource RunningLine}"/> </Canvas> </Grid> </Window> 

Command

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Input; namespace information_system_wpf { class Command : ICommand { private Action<object> execute; private Func<object, bool> canExecute; public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public Command(Action<object> execute, Func<object, bool> canExecute = null) { this.execute = execute; this.canExecute = canExecute; } public bool CanExecute(object parameter) { return this.canExecute == null || this.canExecute(parameter); } public void Execute(object parameter) { this.execute(parameter); } } } 

Viewmodel

 using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading; using System.Windows.Threading; namespace information_system_wpf { class ViewModel : INotifyPropertyChanged { //public ObservableCollection<Model> Models { get; set; } public Model Models { get; set; } public ViewModel() { } private Command addCommand; public Command AddCommand { get { return addCommand ?? (addCommand = new Command(obj => { //Phone phone = new Phone(); //Phones.Insert(0, phone); // SelectedPhone = phone; })); } set { addCommand = value; } } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string prop = "") { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(prop)); } } } 

Model

 using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; namespace information_system_wpf { class Model : INotifyPropertyChanged { private string runline; public string RunLine { get { return runline; } set { runline = value; OnPropertyChanged("RunLine"); } } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string prop = "") { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(prop)); } } } 
  • What exactly is not working? Where is the work with threads here? If you change something in the interface and it does not change in the ViewModel or change something in the ViewModel and it does not change to the UI, then, first of all, check the bindings - tym32167
  • You have three layers: M, VM and V. Between which layers is there no match? The data in the VM does not match the data in M, or is the mapping in V not corresponding to the data in VM? - VladD
  • Yes, and you are attached to the model objects. This is impossible, for example, because a model object has the right to live in its stream. Bind to VM objects. - VladD
  • @ tym32167 It works for me, I speak in the ViewModel in the constructor, just fill the object and display it in the UI. And I need the thread to read, say, updates of the UI from the database, the properties are changed and the UI is updated. I tried to transmit the data through the delegate, in a separate stream, but the window hangs until it reaches the last change. - Identin
  • @VladD I meant, for example, already attached data when reading new ones so that they are updated. I did it in a separate thread through the delegates in the ViewModel, but it still hung until the last item was considered. - Identin

0