2015-10-15 60 views
1

我已经得到了我的命令以下设置。我似乎无法弄清楚我如何引用我的按钮所在的窗口,以便我可以关闭它。发送窗口作为命令参数

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

(上MainWindow.xaml)按钮

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

这里是我的命令,分别位于

类> 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

我试过这样的,它为我工作:

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

记住,辅助方法是一个扩展方法,把它放在公共静态类中。

问候

+0

我们有一个非常方便的方法'Window.GetWindow(...)',在这种情况下,它应该是简单的像这样'parentWindow = Window.GetWindow(DOBJ);',而不必走了你自己的视觉树。 –

+1

@KingKing谢谢。 – Ilan