Martyn Ball
Martyn Ball

Reputation: 4885

Send Window as command parameter

I have got the following setup for my commands. I can't seem to figure out how I reference the window which my button is on, so that I can close it.

Is there some way I can use the command arguments ExecutedRoutedEventArgs e to reference the window and close it?

Button (on MainWindow.xaml)

<Button Command="Commands:MyCommands.CloseWindow">&#10006;</Button>

Here are my commands, which are located in

Classes > Commands.cs

namespace Duplicate_Deleter.Classes
{
    public class MyCommands
    {
        private static RoutedUICommand _CloseWindow;
        private static RoutedUICommand _MinimizeWindow;

        static MyCommands()
        {
            _CloseWindow = new RoutedUICommand("Close current window",
                            "CloseWindow", typeof(MyCommands));
            _MinimizeWindow = new RoutedUICommand("Minimize current window",
                            "MinimizeWindow", typeof(MyCommands));
        }

        public static void BindCommandsToWindow(Window window)
        {
            window.CommandBindings.Add(
                new CommandBinding(CloseWindow, CloseWindow_Executed, CloseWindow_CanExecute));
            window.CommandBindings.Add(
                new CommandBinding(MinimizeWindow, MinimizeWindow_Executed, MinimizeWindow_CanExecute));
        }

        // Command: CloseWindow
        public static RoutedUICommand CloseWindow
        {
            get { return _CloseWindow; }
        }
        public static void CloseWindow_Executed(object sender,
                   ExecutedRoutedEventArgs e)
        {
            //Close window using e?
        }
        public static void CloseWindow_CanExecute(object sender,
                           CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        // Command: MinimizeWindow
        public static RoutedUICommand MinimizeWindow
        {
            get { return _MinimizeWindow; }
        }
        public static void MinimizeWindow_Executed(object sender,
                   ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Minimize Window");
        }
        public static void MinimizeWindow_CanExecute(object sender,
                           CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }
    }
}

I bind the commands using a customized startup in

App.xaml.cs

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        //Startup
        Window main = new MainWindow();
        main.Show();

        //Bind Commands
        Classes.MyCommands.BindCommandsToWindow(main);
    }
}

Upvotes: 1

Views: 131

Answers (1)

Ilan
Ilan

Reputation: 2782

I tried this way and it works for me:

        private void NewCommand_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        var dObj = e.Source as DependencyObject;
        if(dObj == null) return;
        var parentWindow = dObj.GetVisualParentOfType<Window>();
        if(parentWindow == null) 
            return;
        parentWindow.Close();
    }

Helper:

    public static T GetVisualParentOfType<T>(this DependencyObject child)
        where T : DependencyObject
    {
        var parentObject = VisualTreeHelper.GetParent(child);
        if (parentObject == null) return null;
        var parent = parentObject as T;
        return parent ?? GetVisualParentOfType<T>(parentObject);
    }

keep in mind that the helper method is an extension method that, put it in public static class.

regards

Upvotes: 1

Related Questions