1
我已经得到了我的命令以下设置。我似乎无法弄清楚我如何引用我的按钮所在的窗口,以便我可以关闭它。发送窗口作为命令参数
有什么方法可以使用命令参数ExecutedRoutedEventArgs e
来引用窗口并关闭它?
(上MainWindow.xaml)按钮
<Button Command="Commands:MyCommands.CloseWindow">✖</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);
}
}
我们有一个非常方便的方法'Window.GetWindow(...)',在这种情况下,它应该是简单的像这样'parentWindow = Window.GetWindow(DOBJ);',而不必走了你自己的视觉树。 –
@KingKing谢谢。 – Ilan