【问题标题】:WPF Desktop app - Failing to disable Maximize even when ResizeMode is CanMinimizeWPF 桌面应用程序 - 即使 ResizeMode 为 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

【问题讨论】:

    标签: c# wpf windows


    【解决方案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
            }
    

    【讨论】:

      猜你喜欢
      • 2019-05-15
      • 2017-10-29
      • 2017-11-10
      • 2014-01-30
      • 2012-10-20
      • 2020-04-23
      • 2012-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多