WPF 窗口未最小化/最大化

发布于 2025-01-03 23:56:12 字数 1553 浏览 2 评论 0原文

我是一名 WPF 程序员新手。我的问题如下。

我有一个 WPF 应用程序,在主屏幕上,当我单击最大化按钮时,它不会最大化屏幕,而是将其重新定位到左上列。如果我通过单击和拖动来增加/减小屏幕尺寸,则最大化按钮可以正常工作。窗口属性设置如下:

<Window x:Class="MyApp.Home" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        x:Uid="Window_Main" 
        x:Name="HomeWindow" 
        WindowStyle="None" 
        AllowsTransparency="True" 
        WindowStartupLocation="CenterScreen" 
        Margin="0" 
        BorderBrush="Transparent" 
        BorderThickness="0" 
        Title="Home" 
        MinWidth="1000" MinHeight="800"  
        ResizeMode="CanResizeWithGrip" 
        HorizontalAlignment="Stretch" 
        VerticalAlignment="Stretch" 
        SizeToContent="WidthAndHeight" 
        Visibility="Visible" 
        d:DesignHeight="1080" d:DesignWidth="1200">

按钮的 XAML 为:

<Button x:Uid="btnRestore" x:Name="btnRestore" Height="20" Width="20" Style="{DynamicResource Control_WindowControlBoxStyle}"  Margin="5,0,0,0" ToolTip="Toggle Restore" Click="btnRestore_Click" />

btnRestore_Click 的代码很简单:

    private void btnRestore_Click(object sender, RoutedEventArgs e)
    {
        WindowState = WindowState.Maximized;      
    }

我确信我犯了一些愚蠢的错误,但无法弄清楚那是什么。看似简单,但却让我感到困惑。任何帮助将不胜感激。

I am a novice WPF programmer. My problem is as below.

I have a WPF application and on the home screen, when I click on the maximize button, it does not maximize the screen, instead repositions it to the top left column. If I increase/decrease the screen size through click and drag, the maximize button works just fine. The window properties are set up as follows:

<Window x:Class="MyApp.Home" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        x:Uid="Window_Main" 
        x:Name="HomeWindow" 
        WindowStyle="None" 
        AllowsTransparency="True" 
        WindowStartupLocation="CenterScreen" 
        Margin="0" 
        BorderBrush="Transparent" 
        BorderThickness="0" 
        Title="Home" 
        MinWidth="1000" MinHeight="800"  
        ResizeMode="CanResizeWithGrip" 
        HorizontalAlignment="Stretch" 
        VerticalAlignment="Stretch" 
        SizeToContent="WidthAndHeight" 
        Visibility="Visible" 
        d:DesignHeight="1080" d:DesignWidth="1200">

The XAML for button is:

<Button x:Uid="btnRestore" x:Name="btnRestore" Height="20" Width="20" Style="{DynamicResource Control_WindowControlBoxStyle}"  Margin="5,0,0,0" ToolTip="Toggle Restore" Click="btnRestore_Click" />

And the code for btnRestore_Click is simple:

    private void btnRestore_Click(object sender, RoutedEventArgs e)
    {
        WindowState = WindowState.Maximized;      
    }

I am sure there must be some silly mistake that I am committing but not able to figure out what that is. Seems simple but caught me in spin. Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我做我的改变 2025-01-10 23:56:12

您有 SizeToContent="WidthAndHeight",这将使窗口大小调整为窗口中内容的大小。

删除此属性,或者确保 Window 标记的第一个子级没有设置大小

You have SizeToContent="WidthAndHeight", which will make the window resize to whatever size the content in the Window is.

Either remove this property, or make sure first child of your Window tag doesn't have a size set

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文