Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <Window x:Class="CommandMVVM.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:local="clr-namespace:CommandMVVM"
- mc:Ignorable="d"
- Title="MainWindow" Height="450" Width="800">
- <Window.Resources>
- <local:MainWindowViewModel x:Key="MainWindowViewModel" />
- </Window.Resources>
- <Grid DataContext="{StaticResource MainWindowViewModel}">
- <Grid.RowDefinitions>
- <RowDefinition Height="*"/>
- <RowDefinition Height="30"/>
- </Grid.RowDefinitions>
- <Button Name="v_Button_ToggleNextWindow" Grid.Row="1" Command="{Binding StartStopCommand, Mode=OneTime}"
- VerticalAlignment="Center" HorizontalAlignment="Center" Width="90">
- <Button.Content>
- <TextBlock Text="{Binding ButtonContent}" />
- </Button.Content>
- </Button>
- </Grid>
- public class MainWindowViewModel : BaseViewModel
- {
- SecondWindow rg;
- /// <summary>
- /// Комманда для кнопки открывающей второе вью
- /// </summary>
- //private ICommand _toggleSecondViewCommand;
- //public ICommand ToggleSecondViewCommand
- //{
- // get
- // {
- // return _toggleSecondViewCommand;
- // }
- // private set { }
- //}
- /// <summary>
- /// Надпись на кнопке
- /// </summary>
- private string _buttonContent;
- public string ButtonContent
- {
- get => _buttonContent;
- set
- {
- _buttonContent = value;
- NotifyPropertyChanged("ButtonContent");
- }
- }
- /// <summary>
- /// Флаг
- /// </summary>
- private bool _IsRunning;
- public bool IsRunning
- {
- get => _IsRunning;
- set
- {
- _IsRunning = value;
- //обновление кнопки
- StartStopCommand.RaiseCanExecuteChanged();
- }
- }
- /// <summary>
- /// Кнопка запуска и останова
- /// </summary>
- private RelayCommand _StartStopCommand;
- public RelayCommand StartStopCommand
- {
- // get {return }
- get => _StartStopCommand = _StartStopCommand ?? new RelayCommand(ToggleSecondViewExecute, CanStartStop);
- }
- /// <summary>
- ///.ctor
- /// </summary>
- public MainWindowViewModel()
- {
- //InitializeCommands();
- }
- /// <summary>
- /// Start/Stop flag
- /// </summary>
- /// <returns></returns>
- private bool CanStartStop()
- {
- if (IsRunning)
- {
- ButtonContent = "Стоп";
- }
- else
- {
- ButtonContent = "Старт";
- }
- return true;
- }
- private void ToggleSecondViewExecute()
- {
- Boolean windowOpened = false;
- if (IsRunning && rg != null)
- {
- IsRunning = false;
- }
- else
- {
- if (!windowOpened)
- {
- windowOpened = true;
- IsRunning = true;
- rg = new SecondWindow();
- rg.Show();
- Debug.WriteLine($"IsRunning = {IsRunning} ButtonContent = {ButtonContent}");
- }
- }
- }
- // Deprecated
- //private void InitializeCommands()
- //{
- // _toggleSecondViewCommand = new RelayCommand(ToggleSecondViewExecute);
- //}
- }
- public class RelayCommand : ICommand
- {
- Action _targetExecuteMethod;
- Func<bool> _targetCanExecuteMethod;
- public RelayCommand(Action executeMethod)
- {
- _targetExecuteMethod = executeMethod;
- }
- public RelayCommand(Action executeMethod, Func<bool> canExecuteMethod)
- {
- _targetExecuteMethod = executeMethod;
- _targetCanExecuteMethod = canExecuteMethod;
- }
- public void RaiseCanExecuteChanged()
- {
- CanExecuteChanged(this, EventArgs.Empty);
- }
- #region ICommand Members
- bool ICommand.CanExecute(object parameter)
- {
- if (_targetCanExecuteMethod != null)
- {
- return _targetCanExecuteMethod();
- }
- if (_targetExecuteMethod != null)
- {
- return true;
- }
- return false;
- }
- // Beware - should use weak references if command instance lifetime is longer than lifetime
- // of UI objects that get hooked up to command
- // Prism commands solve this in their implementation
- public event EventHandler CanExecuteChanged = delegate { };
- void ICommand.Execute(object parameter)
- {
- if (_targetExecuteMethod != null)
- {
- _targetExecuteMethod();
- }
- }
- #endregion
- }
- public class RelayCommand<T> : ICommand
- {
- Action<T> _targetExecuteMethod;
- Func<T, bool> _targetCanExecuteMethod;
- public RelayCommand(Action<T> executeMethod)
- {
- _targetExecuteMethod = executeMethod;
- }
- public RelayCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod)
- {
- _targetExecuteMethod = executeMethod;
- _targetCanExecuteMethod = canExecuteMethod;
- }
- public void RaiseCanExecuteChanged()
- {
- CanExecuteChanged(this, EventArgs.Empty);
- }
- #region ICommand Members
- bool ICommand.CanExecute(object parameter)
- {
- if (_targetCanExecuteMethod != null)
- {
- T tparm = (T)parameter;
- return _targetCanExecuteMethod(tparm);
- }
- if (_targetExecuteMethod != null)
- {
- return true;
- }
- return false;
- }
- // Beware - should use weak references if command instance lifetime is longer than lifetime of
- // UI objects that get hooked up to command
- // Prism commands solve this in their implementation
- public event EventHandler CanExecuteChanged = delegate { };
- void ICommand.Execute(object parameter)
- {
- if (_targetExecuteMethod != null)
- {
- _targetExecuteMethod((T)parameter);
- }
- }
- #endregion
- }
- public abstract class NotificationObject : INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
- protected virtual void NotifyPropertyChanged(string propertyName)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- protected void SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
- {
- if (!EqualityComparer<T>.Default.Equals(field, value))
- {
- field = value;
- NotifyPropertyChanged(propertyName);
- }
- }
- }
- public class BaseViewModel : NotificationObject
- {
- }
- <Button Width="60" Height="30" Command="{Binding SimpleCommand}">
- <Button.Style>
- <Style TargetType="{x:Type Button}">
- <!-- Triggered values -->
- <Style.Triggers>
- <DataTrigger Binding="{Binding Test.IsRunning}" Value="True">
- <Setter Property="Content" Value="Стоп"/>
- <Setter Property="IsEnabled" Value="False"/>
- </DataTrigger>
- <DataTrigger Binding="{Binding Test.IsRunning}" Value="False">
- <Setter Property="Content" Value="Старт"/>
- <Setter Property="IsEnabled" Value="True"/>
- </DataTrigger>
- </Style.Triggers>
- </Style>
- </Button.Style>
- </Button>
- public class TestViewModel : INotifyPropertyChanged
- {
- private bool _isRunning;
- public bool IsRunning
- {
- get => _isRunning;
- set
- {
- _isRunning = value;
- OnPropertyChanged();
- }
- }
- public event PropertyChangedEventHandler PropertyChanged;
- protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- public TestViewModel Test { get; set; } = new TestViewModel();
- public MainWindow()
- {
- InitializeComponent();
- SimpleCommand = new RelayCommand(DoSimpleCommand);
- DataContext = this;
- }
- private SecondWindow rg;
- private void DoSimpleCommand()
- {
- Test.IsRunning = true;
- if (rg == null || rg.IsClosed)
- {
- rg = new SecondWindow(Test);
- }
- rg.Show();
- }
- public RelayCommand SimpleCommand { get; }
- private TestViewModel Test { get; }
- public SecondWindow(TestViewModel test)
- {
- InitializeComponent();
- Test = test;
- }
- public bool IsClosed { get; private set; }
- protected override void OnClosed(EventArgs e)
- {
- base.OnClosed(e);
- Test.IsRunning = false;
- IsClosed = true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement