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)); } } }