发送窗口作为命令参数

时间:2015-10-15 11:55:27

标签: c# .net wpf xaml

我的命令设置如下。我似乎无法弄清楚我如何引用我的按钮所在的窗口,以便我可以关闭它。

有什么方法可以使用命令参数ExecutedRoutedEventArgs e来引用窗口并关闭它吗?

按钮(在MainWindow.xaml上)

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

以下是我的命令,位于

课程&gt; 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;
        }
    }
}

我使用

中的自定义启动绑定命令

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);
    }
}

1 个答案:

答案 0 :(得分:1)

我试过这种方式,它对我有用:

        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();
    }

帮助者:

    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);
    }

请记住,辅助方法是一种扩展方法,将其放在公共静态类中。

问候