Click here to Skip to main content
13,554,127 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello, all

I have a question. How can I implement converting of the Event in the method based ICommand without using System.Window.Interactivity (Blend 3 SDK). Several ways in the Internet have led to the solution when need this library.

E.g. I have a method based on ICommand interface which bind to menuitem Exit (Exit from application with saving results etc.). And I want same method (based on ICommand) bind to close/closing window events but I need cast command to event.

Thanks for solutions
Posted 1-Feb-13 2:17am
Updated 1-Feb-13 5:44am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The MVVM Light toolkit has an EventToCommand class that will do the trick. It even supports passing the parameters for the event to the command method. I've used it many times and it works great. If you manually include the toolkit (rather than using NuGet) you can just bring in that functionality and not the other stuff like the ViewModelLocator.

Note, however, that the MVVM Light toolkit requires you to include the fact you are using it if you release your application publicly. Refer to the license info on the web site for more details.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I found solution but it works with general events based on class Delegate. It method doesn't work with MulticastDelegate's based methods (e.g. closed, closing window events). Remaining to use routed events or lib System.Windows.Interactivity or MVVM light toolkit (thanks for it solution Jason Gleim[^]).

If interestingly below articles where I took info about commands and events:
1) Commands in MVVM[^]
2) Wire any WPF Event to Command on ViewModel[^]
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

OK, I implement my closed/closing behaviour

Implementation of the closing event
public partial class Commands
        #region Closing (Window event)
        // Event to command
        private static void Window_Closing(object sender, CancelEventArgs e)
            FrameworkElement element = sender as FrameworkElement;
            if (element == null) return;
            ICommand command = GetClosingCommand(element);
            if(command != null && command.CanExecute(e))

        // Property
        public static readonly DependencyProperty ClosingCommandProperty =
            DependencyProperty.RegisterAttached("ClosingCommand", typeof(ICommand),
            typeof(Commands), new PropertyMetadata(default(ICommand), OnClosingCommandChanged));

        public static ICommand GetClosingCommand(DependencyObject d) { return (ICommand)d.GetValue(ClosingCommandProperty); }
        public static void SetClosingCommand(DependencyObject d, ICommand value) { d.SetValue(ClosingCommandProperty, value); }

        // Event (add or remove)
        private static void OnClosingCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            var element = d as Window;
            if (element != null)
                if (e.OldValue == null && e.NewValue != null)
                    element.Closing += Window_Closing;
                else if (e.OldValue != null && e.NewValue == null)
                    element.Closing -= Window_Closing;

Here's we are reating a dependency property and its get and set methods. Then we emplement a ChangeProperty handle where we add or remove our EventHandle to a framework element (in our case it's window class). Event handler just execute a command with CancelEventArgs param.

E.g. of the command
public ICommand ClosingWindowCommand
                return new RelayCommand<canceleventargs>((e)=>
                    MessageBoxResult result = MessageBox.Show(
                        "Are you sure that want to exit from application?",
                        "Exit from application",
                        MessageBoxButton.YesNo, MessageBoxImage.Question);

                    if (result == MessageBoxResult.No) e.Cancel = true;

Just shows message box with two buttons (Yes, No). If user pressed button No then assign to property Cancel of the CancelEventArgs param value true.

XAML (changing our property and binding command)
<window x:class="MyApplication.View.MainWindow" xmlns:x="#unknown">
        DataContext="{Binding CloseTab,ElementName=MainWindowInstance}"
        Title="MainWindow" Height="350" Width="525"
        cmd:Commands.ClosingCommand="{Binding ClosingWindowCommand}">
<!-- TODO MainWindow -->

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.180515.1 | Last Updated 5 Feb 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100