Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- using System.Windows;
- namespace WpfEventBinding
- {
- public static class WindowBindableProperties
- {
- #region ViewModelTerminatingEventProperty
- /// <summary>
- /// Register the ViewModelTerminatingEvent custom DependencyProperty.
- /// </summary>
- private static DependencyProperty _viewModelTerminatingEventProperty =
- DependencyProperty.RegisterAttached
- (
- "ViewModelTerminatingEvent",
- typeof(ViewModelTerminatingEventHandler),
- typeof(WindowBindableProperties),
- new PropertyMetadata(null, ViewModelTerminatingEventPropertyChanged)
- );
- /// <summary>
- /// Identifies the ViewModelTerminatingEvent dependency property.
- /// </summary>
- public static DependencyProperty ViewModelTerminatingEventProperty
- { get { return _viewModelTerminatingEventProperty; } }
- /// <summary>
- /// Gets the attached ViewModelTerminatingEvent dependecy property.
- /// </summary>
- /// <param name="dependencyObject">The window attached to the WindowViewModel.</param>
- /// <returns>The ViewModelTerminatingEventHandler bound to this property</returns>
- public static ViewModelTerminatingEventHandler GetViewModelTerminatingEvent
- (DependencyObject dependencyObject)
- {
- return (dependencyObject.GetValue(ViewModelTerminatingEventProperty)
- as ViewModelTerminatingEventHandler);
- }
- /// <summary>
- /// Sets the ViewModelTerminatingEvent dependency property.
- /// </summary>
- public static void SetViewModelTerminatingEvent(
- DependencyObject dependencyObject,
- ViewModelTerminatingEventHandler value)
- {
- dependencyObject.SetValue(ViewModelTerminatingEventProperty, value);
- }
- /// <summary>
- /// Gets the ViewModelTerminatingEvent dependency property.
- /// </summary>
- private static void ViewModelTerminatingEventPropertyChanged(
- DependencyObject d,
- DependencyPropertyChangedEventArgs e)
- {
- Window instance = d as Window;
- if (null != instance)
- {
- if (null != e.OldValue)
- {
- throw new System.InvalidOperationException(
- "ViewModelTerminatingEvent dependency property cannot be changed.");
- }
- if (null != e.NewValue)
- {
- // Attach the Window.Close() method to the ViewModel's event
- var newEvent = (e.NewValue as ViewModelTerminatingEventHandler);
- newEvent += new ViewModelTerminatingEventHandler(() => instance.Close());
- }
- }
- }
- #endregion
- }
- }
- <Window x:Class="WpfEventBinding.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:v="clr-namespace:WpfEventBinding"
- v:WindowBindableProperties.ViewModelTerminatingEvent="{Binding Path=RequestCloseEvent}"
- Title="MainWindow" Height="350" Width="525">
- <Grid>
- <Button Content="{Binding Path=CloseCommandName}" Click="StopButton_Click" ></Button>
- </Grid>
- </Window>
- using System.Windows;
- namespace WpfEventBinding
- {
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
- private void StopButton_Click(object sender, RoutedEventArgs e)
- {
- MainWindowViewModel vm = (DataContext as MainWindowViewModel);
- if (null != vm)
- {
- vm.Stop();
- }
- }
- }
- }
- using System;
- using System.ComponentModel;
- namespace WpfEventBinding
- {
- public delegate void ViewModelTerminatingEventHandler();
- class MainWindowViewModel
- : INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
- // Raised by the ViewModel to indicate to the view that it is no longer required.
- // Causes System.Windows.Data Error: 40 : BindingExpression path error. Is it
- // Possible to bind to an 'event' property?
- public event ViewModelTerminatingEventHandler RequestCloseEvent;
- // This has to have the public 'get' to allow binding. Is there some way to
- // do the same thing for the 'event'?
- public String CloseCommandName { get; private set; }
- public MainWindowViewModel()
- {
- CloseCommandName = "Close";
- }
- internal void Stop()
- {
- ViewModelTerminatingEventHandler RaiseRequestCloseEvent =
- RequestCloseEvent;
- if (null != RaiseRequestCloseEvent)
- {
- RaiseRequestCloseEvent();
- }
- }
- internal void Start()
- {
- OnPropertyChanged("CloseCommandName");
- OnPropertyChanged("ViewModelTerminatingEvent");
- }
- private void OnPropertyChanged(String propertyName)
- {
- PropertyChangedEventHandler RaisePropertyChangedEvent = PropertyChanged;
- if (RaisePropertyChangedEvent != null)
- {
- var propertyChangedEventArgs = new PropertyChangedEventArgs(propertyName);
- RaisePropertyChangedEvent(this, propertyChangedEventArgs);
- }
- }
- }
- }
- <Application x:Class="WpfEventBinding.App"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- >
- <Application.Resources>
- <!-- Nothing to see here. Move along... -->
- </Application.Resources>
- </Application>
- using System.Windows;
- namespace WpfEventBinding
- {
- public partial class App : Application
- {
- public App()
- {
- Startup += new StartupEventHandler(App_Startup);
- }
- void App_Startup(object sender, StartupEventArgs e)
- {
- MainWindowViewModel vm = new MainWindowViewModel();
- MainWindow window = new MainWindow();
- // Make sure this is set before attempting binding!
- window.DataContext = vm;
- vm.Start();
- window.Show();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement