Advertisement
Guest User

Untitled

a guest
Jul 24th, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Windows;
  4.  
  5. namespace WpfEventBinding
  6. {
  7. public static class WindowBindableProperties
  8. {
  9. #region ViewModelTerminatingEventProperty
  10.  
  11. /// <summary>
  12. /// Register the ViewModelTerminatingEvent custom DependencyProperty.
  13. /// </summary>
  14. private static DependencyProperty _viewModelTerminatingEventProperty =
  15. DependencyProperty.RegisterAttached
  16. (
  17. "ViewModelTerminatingEvent",
  18. typeof(ViewModelTerminatingEventHandler),
  19. typeof(WindowBindableProperties),
  20. new PropertyMetadata(null, ViewModelTerminatingEventPropertyChanged)
  21. );
  22.  
  23. /// <summary>
  24. /// Identifies the ViewModelTerminatingEvent dependency property.
  25. /// </summary>
  26. public static DependencyProperty ViewModelTerminatingEventProperty
  27. { get { return _viewModelTerminatingEventProperty; } }
  28.  
  29. /// <summary>
  30. /// Gets the attached ViewModelTerminatingEvent dependecy property.
  31. /// </summary>
  32. /// <param name="dependencyObject">The window attached to the WindowViewModel.</param>
  33. /// <returns>The ViewModelTerminatingEventHandler bound to this property</returns>
  34. public static ViewModelTerminatingEventHandler GetViewModelTerminatingEvent
  35. (DependencyObject dependencyObject)
  36. {
  37. return (dependencyObject.GetValue(ViewModelTerminatingEventProperty)
  38. as ViewModelTerminatingEventHandler);
  39. }
  40.  
  41. /// <summary>
  42. /// Sets the ViewModelTerminatingEvent dependency property.
  43. /// </summary>
  44. public static void SetViewModelTerminatingEvent(
  45. DependencyObject dependencyObject,
  46. ViewModelTerminatingEventHandler value)
  47. {
  48. dependencyObject.SetValue(ViewModelTerminatingEventProperty, value);
  49. }
  50.  
  51. /// <summary>
  52. /// Gets the ViewModelTerminatingEvent dependency property.
  53. /// </summary>
  54. private static void ViewModelTerminatingEventPropertyChanged(
  55. DependencyObject d,
  56. DependencyPropertyChangedEventArgs e)
  57. {
  58. Window instance = d as Window;
  59. if (null != instance)
  60. {
  61. if (null != e.OldValue)
  62. {
  63. throw new System.InvalidOperationException(
  64. "ViewModelTerminatingEvent dependency property cannot be changed.");
  65. }
  66.  
  67. if (null != e.NewValue)
  68. {
  69. // Attach the Window.Close() method to the ViewModel's event
  70. var newEvent = (e.NewValue as ViewModelTerminatingEventHandler);
  71. newEvent += new ViewModelTerminatingEventHandler(() => instance.Close());
  72. }
  73. }
  74. }
  75.  
  76. #endregion
  77. }
  78. }
  79.  
  80. <Window x:Class="WpfEventBinding.MainWindow"
  81. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  82. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  83. xmlns:v="clr-namespace:WpfEventBinding"
  84. v:WindowBindableProperties.ViewModelTerminatingEvent="{Binding Path=RequestCloseEvent}"
  85. Title="MainWindow" Height="350" Width="525">
  86. <Grid>
  87. <Button Content="{Binding Path=CloseCommandName}" Click="StopButton_Click" ></Button>
  88. </Grid>
  89. </Window>
  90.  
  91. using System.Windows;
  92.  
  93. namespace WpfEventBinding
  94. {
  95. public partial class MainWindow : Window
  96. {
  97. public MainWindow()
  98. {
  99. InitializeComponent();
  100. }
  101.  
  102. private void StopButton_Click(object sender, RoutedEventArgs e)
  103. {
  104. MainWindowViewModel vm = (DataContext as MainWindowViewModel);
  105. if (null != vm)
  106. {
  107. vm.Stop();
  108. }
  109. }
  110. }
  111. }
  112.  
  113. using System;
  114. using System.ComponentModel;
  115.  
  116. namespace WpfEventBinding
  117. {
  118. public delegate void ViewModelTerminatingEventHandler();
  119.  
  120. class MainWindowViewModel
  121. : INotifyPropertyChanged
  122. {
  123. public event PropertyChangedEventHandler PropertyChanged;
  124.  
  125. // Raised by the ViewModel to indicate to the view that it is no longer required.
  126. // Causes System.Windows.Data Error: 40 : BindingExpression path error. Is it
  127. // Possible to bind to an 'event' property?
  128. public event ViewModelTerminatingEventHandler RequestCloseEvent;
  129.  
  130. // This has to have the public 'get' to allow binding. Is there some way to
  131. // do the same thing for the 'event'?
  132. public String CloseCommandName { get; private set; }
  133.  
  134. public MainWindowViewModel()
  135. {
  136. CloseCommandName = "Close";
  137. }
  138.  
  139. internal void Stop()
  140. {
  141. ViewModelTerminatingEventHandler RaiseRequestCloseEvent =
  142. RequestCloseEvent;
  143. if (null != RaiseRequestCloseEvent)
  144. {
  145. RaiseRequestCloseEvent();
  146. }
  147. }
  148.  
  149. internal void Start()
  150. {
  151. OnPropertyChanged("CloseCommandName");
  152. OnPropertyChanged("ViewModelTerminatingEvent");
  153. }
  154.  
  155. private void OnPropertyChanged(String propertyName)
  156. {
  157. PropertyChangedEventHandler RaisePropertyChangedEvent = PropertyChanged;
  158. if (RaisePropertyChangedEvent != null)
  159. {
  160. var propertyChangedEventArgs = new PropertyChangedEventArgs(propertyName);
  161. RaisePropertyChangedEvent(this, propertyChangedEventArgs);
  162. }
  163. }
  164. }
  165. }
  166.  
  167. <Application x:Class="WpfEventBinding.App"
  168. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  169. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  170. >
  171. <Application.Resources>
  172. <!-- Nothing to see here. Move along... -->
  173. </Application.Resources>
  174. </Application>
  175.  
  176. using System.Windows;
  177.  
  178. namespace WpfEventBinding
  179. {
  180. public partial class App : Application
  181. {
  182. public App()
  183. {
  184. Startup += new StartupEventHandler(App_Startup);
  185. }
  186.  
  187. void App_Startup(object sender, StartupEventArgs e)
  188. {
  189. MainWindowViewModel vm = new MainWindowViewModel();
  190. MainWindow window = new MainWindow();
  191.  
  192. // Make sure this is set before attempting binding!
  193. window.DataContext = vm;
  194. vm.Start();
  195. window.Show();
  196. }
  197. }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement