WPF 桌面应用程序 - 即使 ResizeMode 为 CanMinimize 也无法禁用最大化
Posted
技术标签:
【中文标题】WPF 桌面应用程序 - 即使 ResizeMode 为 CanMinimize 也无法禁用最大化【英文标题】:WPF Desktop app - Failing to disable Maximize even when ResizeMode is CanMinimize 【发布时间】:2021-12-01 11:17:08 【问题描述】:C# WPF 桌面应用程序 - 即使在自定义窗口控件中 ResizeMode 为 CanMinimize 时,也无法禁用最大化。图片符合预期:
尝试在控件的上部声明中在 XAML 中设置 ResizeMode="CanMinimize"。甚至尝试放入控制构造函数:
public MaterialWindow()
WindowChrome.SetWindowChrome(this, new WindowChrome()
CaptionHeight = 32,
NonClientFrameEdges = NonClientFrameEdges.None,
ResizeBorderThickness = new Thickness(4),
UseAeroCaptionButtons = true
);
ResizeMode = ResizeMode.CanMinimize; // <=
CommandBindings.Add(new CommandBinding(SystemCommands.CloseWindowCommand, CloseWindow_Exec, CloseWindow_CanExec));
CommandBindings.Add(new CommandBinding(SystemCommands.MinimizeWindowCommand, MinimizeWindow_Exec, MinimizeWindow_CanExec));
CommandBindings.Add(new CommandBinding(SystemCommands.MaximizeWindowCommand, MaximizeWindow_Exec, MaximizeWindow_CanExec));
CommandBindings.Add(new CommandBinding(SystemCommands.RestoreWindowCommand, RestoreWindow_Exec, RestoreWindow_CanExec));
CommandBindings.Add(new CommandBinding(SystemCommands.ShowSystemMenuCommand, ShowSystemMenu_Exec, ShowSystemMenu_CanExec));
甚至尝试在自定义窗口构建后调用设置 ResizeMode 的方法:
ResizeMode = ResizeMode.CanMinimize; // window can only be minimized and restored
【问题讨论】:
【参考方案1】:通过清除命令绑定最大化来修复。显然,如果绑定到位,它将不会遵守 ResizeMode.CanMinimize:
public void DisableMaximize()
// clear the command bindings and set all EXCEPT MAXIMIZE command
CommandBindings.Clear();
CommandBindings.Add(new CommandBinding(SystemCommands.CloseWindowCommand, CloseWindow_Exec, CloseWindow_CanExec));
CommandBindings.Add(new CommandBinding(SystemCommands.MinimizeWindowCommand, MinimizeWindow_Exec, MinimizeWindow_CanExec));
//CommandBindings.Add(new CommandBinding(SystemCommands.MaximizeWindowCommand, MaximizeWindow_Exec, MaximizeWindow_CanExec));
CommandBindings.Add(new CommandBinding(SystemCommands.RestoreWindowCommand, RestoreWindow_Exec, RestoreWindow_CanExec));
CommandBindings.Add(new CommandBinding(SystemCommands.ShowSystemMenuCommand, ShowSystemMenu_Exec, ShowSystemMenu_CanExec));
this.ResizeMode = ResizeMode.CanMinimize; // window can only be minimized and restored
【讨论】:
以上是关于WPF 桌面应用程序 - 即使 ResizeMode 为 CanMinimize 也无法禁用最大化的主要内容,如果未能解决你的问题,请参考以下文章
有没有办法我可以一直在桌面上绘制一个透明的 WPF 应用程序?