Изменение данных в потоке

284
08 сентября 2017, 19:32

Начал изучать MVVM и столкнулся с недопониманием. Например чтобы записать данные из ViewModel во View , необходимо их заполнить в конструкторе ViewModel(). А если мне необходимо в потоке изменять данные и чтобы они изменялись во View. Попытался сделать чтобы делегат транслировал в отдельном потоке, но данные не изменяются.

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));
        }
    }
}
READ ALSO
Что такое Claims и как они работают ?

Что такое Claims и как они работают ?

Какое практическое примение Claims в AspNet Identity

263
Установить иконку в TabControl справа

Установить иконку в TabControl справа

Подскажите пожалуйста, как установить иконку во вкладке TabControl с правой стороны?

245
Валидация SecurityStamp в Json web token

Валидация SecurityStamp в Json web token

Вопрос: Как "правильно" валидировать SecurityStamp в JWT

217