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