我一直在玩WPF / XAML并注意到似乎没有一种简单的方法来编写仅限UI的键盘/鼠标等。例如,如何在窗口中添加“Maximize without windowframe”命令。
目前,这涉及与代码隐藏中的代码不同的代码:
public partial class MainWindow : Window {
public static RoutedUICommand Maximize = new RoutedUICommand("Maximizes window.", "Maximize", typeof(MainWindow));
public MainWindow() {
Loaded += MainWindow_AddCommands;
InitializeComponent();
}
void MainWindow_AddCommands(object sender, RoutedEventArgs e) {
var CanExecuteMaximize = new CanExecuteRoutedEventHandler((obj, args) => {
if (!(WindowState == WindowState.Minimized))
args.CanExecute = true;
else
args.CanExecute = false;
});
var ExecutedMaximize = new ExecutedRoutedEventHandler((obj, args) => {
switch (WindowStyle) {
case WindowStyle.None:
WindowStyle = WindowStyle.SingleBorderWindow;
break;
case WindowStyle.SingleBorderWindow:
WindowStyle = WindowStyle.None;
break;
default:
break;
}
switch (WindowState) {
case WindowState.Maximized:
WindowState = WindowState.Normal;
break;
case WindowState.Minimized:
throw new InvalidOperationException();
case WindowState.Normal:
WindowState = System.Windows.WindowState.Maximized;
break;
default:
break;
}
});
CommandBindings.Add(new CommandBinding(Maximize, ExecutedMaximize, CanExecuteMaximize));
}
}
这在XAML:
<Window.InputBindings>
<KeyBinding Command="{x:Static local:MainWindow.Maximize}" Gesture="F11"/>
</Window.InputBindings>
但这似乎完全是落后的。代码隐藏根本不应该处理UI!看起来这可以通过创建一个依赖属性而不是使用EventTriggers来部分缓解,但是这仍然会使一些代码落后于它看起来不应该存在。
有没有办法在没有代码的情况下进行适当的F11最大化?