Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.54 KB | None | 0 0
  1. <Window x:Class="CommandMVVM.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:local="clr-namespace:CommandMVVM"
  7. mc:Ignorable="d"
  8. Title="MainWindow" Height="450" Width="800">
  9. <Window.Resources>
  10. <local:MainWindowViewModel x:Key="MainWindowViewModel" />
  11. </Window.Resources>
  12. <Grid DataContext="{StaticResource MainWindowViewModel}">
  13. <Grid.RowDefinitions>
  14. <RowDefinition Height="*"/>
  15. <RowDefinition Height="30"/>
  16. </Grid.RowDefinitions>
  17.  
  18. <Button Name="v_Button_ToggleNextWindow" Grid.Row="1" Command="{Binding StartStopCommand, Mode=OneTime}"
  19. VerticalAlignment="Center" HorizontalAlignment="Center" Width="90">
  20. <Button.Content>
  21. <TextBlock Text="{Binding ButtonContent}" />
  22. </Button.Content>
  23. </Button>
  24.  
  25. </Grid>
  26.  
  27. public class MainWindowViewModel : BaseViewModel
  28. {
  29. SecondWindow rg;
  30.  
  31. /// <summary>
  32. /// Комманда для кнопки открывающей второе вью
  33. /// </summary>
  34. //private ICommand _toggleSecondViewCommand;
  35. //public ICommand ToggleSecondViewCommand
  36. //{
  37. // get
  38. // {
  39. // return _toggleSecondViewCommand;
  40. // }
  41.  
  42. // private set { }
  43. //}
  44.  
  45. /// <summary>
  46. /// Надпись на кнопке
  47. /// </summary>
  48. private string _buttonContent;
  49. public string ButtonContent
  50. {
  51. get => _buttonContent;
  52. set
  53. {
  54. _buttonContent = value;
  55. NotifyPropertyChanged("ButtonContent");
  56. }
  57. }
  58.  
  59. /// <summary>
  60. /// Флаг
  61. /// </summary>
  62. private bool _IsRunning;
  63. public bool IsRunning
  64. {
  65. get => _IsRunning;
  66. set
  67. {
  68. _IsRunning = value;
  69. //обновление кнопки
  70. StartStopCommand.RaiseCanExecuteChanged();
  71. }
  72. }
  73.  
  74.  
  75. /// <summary>
  76. /// Кнопка запуска и останова
  77. /// </summary>
  78. private RelayCommand _StartStopCommand;
  79. public RelayCommand StartStopCommand
  80. {
  81. // get {return }
  82. get => _StartStopCommand = _StartStopCommand ?? new RelayCommand(ToggleSecondViewExecute, CanStartStop);
  83. }
  84.  
  85. /// <summary>
  86. ///.ctor
  87. /// </summary>
  88. public MainWindowViewModel()
  89. {
  90. //InitializeCommands();
  91. }
  92.  
  93. /// <summary>
  94. /// Start/Stop flag
  95. /// </summary>
  96. /// <returns></returns>
  97. private bool CanStartStop()
  98. {
  99. if (IsRunning)
  100. {
  101. ButtonContent = "Стоп";
  102. }
  103. else
  104. {
  105. ButtonContent = "Старт";
  106. }
  107.  
  108. return true;
  109. }
  110.  
  111.  
  112. private void ToggleSecondViewExecute()
  113. {
  114. Boolean windowOpened = false;
  115.  
  116. if (IsRunning && rg != null)
  117. {
  118. IsRunning = false;
  119. }
  120.  
  121. else
  122. {
  123. if (!windowOpened)
  124. {
  125.  
  126. windowOpened = true;
  127. IsRunning = true;
  128. rg = new SecondWindow();
  129. rg.Show();
  130. Debug.WriteLine($"IsRunning = {IsRunning} ButtonContent = {ButtonContent}");
  131. }
  132. }
  133. }
  134.  
  135. // Deprecated
  136. //private void InitializeCommands()
  137. //{
  138. // _toggleSecondViewCommand = new RelayCommand(ToggleSecondViewExecute);
  139. //}
  140. }
  141.  
  142. public class RelayCommand : ICommand
  143. {
  144. Action _targetExecuteMethod;
  145. Func<bool> _targetCanExecuteMethod;
  146.  
  147. public RelayCommand(Action executeMethod)
  148. {
  149. _targetExecuteMethod = executeMethod;
  150. }
  151.  
  152. public RelayCommand(Action executeMethod, Func<bool> canExecuteMethod)
  153. {
  154. _targetExecuteMethod = executeMethod;
  155. _targetCanExecuteMethod = canExecuteMethod;
  156. }
  157.  
  158. public void RaiseCanExecuteChanged()
  159. {
  160. CanExecuteChanged(this, EventArgs.Empty);
  161. }
  162. #region ICommand Members
  163.  
  164. bool ICommand.CanExecute(object parameter)
  165. {
  166. if (_targetCanExecuteMethod != null)
  167. {
  168. return _targetCanExecuteMethod();
  169. }
  170. if (_targetExecuteMethod != null)
  171. {
  172. return true;
  173. }
  174. return false;
  175. }
  176.  
  177. // Beware - should use weak references if command instance lifetime is longer than lifetime
  178. // of UI objects that get hooked up to command
  179. // Prism commands solve this in their implementation
  180. public event EventHandler CanExecuteChanged = delegate { };
  181.  
  182. void ICommand.Execute(object parameter)
  183. {
  184. if (_targetExecuteMethod != null)
  185. {
  186. _targetExecuteMethod();
  187. }
  188. }
  189. #endregion
  190. }
  191.  
  192. public class RelayCommand<T> : ICommand
  193. {
  194. Action<T> _targetExecuteMethod;
  195. Func<T, bool> _targetCanExecuteMethod;
  196.  
  197. public RelayCommand(Action<T> executeMethod)
  198. {
  199. _targetExecuteMethod = executeMethod;
  200. }
  201.  
  202. public RelayCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod)
  203. {
  204. _targetExecuteMethod = executeMethod;
  205. _targetCanExecuteMethod = canExecuteMethod;
  206. }
  207.  
  208. public void RaiseCanExecuteChanged()
  209. {
  210. CanExecuteChanged(this, EventArgs.Empty);
  211. }
  212. #region ICommand Members
  213.  
  214. bool ICommand.CanExecute(object parameter)
  215. {
  216. if (_targetCanExecuteMethod != null)
  217. {
  218. T tparm = (T)parameter;
  219. return _targetCanExecuteMethod(tparm);
  220. }
  221. if (_targetExecuteMethod != null)
  222. {
  223. return true;
  224. }
  225. return false;
  226. }
  227.  
  228. // Beware - should use weak references if command instance lifetime is longer than lifetime of
  229. // UI objects that get hooked up to command
  230. // Prism commands solve this in their implementation
  231. public event EventHandler CanExecuteChanged = delegate { };
  232.  
  233. void ICommand.Execute(object parameter)
  234. {
  235. if (_targetExecuteMethod != null)
  236. {
  237. _targetExecuteMethod((T)parameter);
  238. }
  239. }
  240. #endregion
  241. }
  242.  
  243. public abstract class NotificationObject : INotifyPropertyChanged
  244. {
  245. public event PropertyChangedEventHandler PropertyChanged;
  246.  
  247. protected virtual void NotifyPropertyChanged(string propertyName)
  248. {
  249. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  250. }
  251.  
  252. protected void SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
  253. {
  254. if (!EqualityComparer<T>.Default.Equals(field, value))
  255. {
  256. field = value;
  257. NotifyPropertyChanged(propertyName);
  258. }
  259. }
  260. }
  261.  
  262. public class BaseViewModel : NotificationObject
  263. {
  264.  
  265. }
  266.  
  267. <Button Width="60" Height="30" Command="{Binding SimpleCommand}">
  268. <Button.Style>
  269. <Style TargetType="{x:Type Button}">
  270. <!-- Triggered values -->
  271. <Style.Triggers>
  272. <DataTrigger Binding="{Binding Test.IsRunning}" Value="True">
  273. <Setter Property="Content" Value="Стоп"/>
  274. <Setter Property="IsEnabled" Value="False"/>
  275. </DataTrigger>
  276. <DataTrigger Binding="{Binding Test.IsRunning}" Value="False">
  277. <Setter Property="Content" Value="Старт"/>
  278. <Setter Property="IsEnabled" Value="True"/>
  279. </DataTrigger>
  280. </Style.Triggers>
  281. </Style>
  282. </Button.Style>
  283. </Button>
  284.  
  285. public class TestViewModel : INotifyPropertyChanged
  286. {
  287. private bool _isRunning;
  288. public bool IsRunning
  289. {
  290. get => _isRunning;
  291. set
  292. {
  293. _isRunning = value;
  294. OnPropertyChanged();
  295. }
  296. }
  297.  
  298. public event PropertyChangedEventHandler PropertyChanged;
  299.  
  300. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  301. {
  302. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  303. }
  304. }
  305.  
  306. public TestViewModel Test { get; set; } = new TestViewModel();
  307.  
  308. public MainWindow()
  309. {
  310. InitializeComponent();
  311. SimpleCommand = new RelayCommand(DoSimpleCommand);
  312. DataContext = this;
  313. }
  314.  
  315. private SecondWindow rg;
  316. private void DoSimpleCommand()
  317. {
  318. Test.IsRunning = true;
  319. if (rg == null || rg.IsClosed)
  320. {
  321. rg = new SecondWindow(Test);
  322. }
  323.  
  324. rg.Show();
  325. }
  326.  
  327. public RelayCommand SimpleCommand { get; }
  328.  
  329. private TestViewModel Test { get; }
  330.  
  331. public SecondWindow(TestViewModel test)
  332. {
  333. InitializeComponent();
  334. Test = test;
  335. }
  336.  
  337. public bool IsClosed { get; private set; }
  338. protected override void OnClosed(EventArgs e)
  339. {
  340. base.OnClosed(e);
  341. Test.IsRunning = false;
  342. IsClosed = true;
  343. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement