WPF窗口最大化宽度和高度

问题描述 投票:0回答:2

TL; DR

似乎将AllowTransparency设置为True偏移窗口向上并在最大化时保持8像素,我需要将其恢复到屏幕上。

深入

看起来我在某个地方犯了一个错误,但我不确定为什么。我无法发布整个代码,但我会尝试编写与此相关的所有内容。

我有我的主窗口,它有一个自定义标题栏。这意味着我必须将WindowsStyle设置为None,我也使用模板。从一开始,我遇到的问题是窗口比我的桌面大得多,同时最大化了。在所有方面,屏幕偏离了8像素。我放弃了模板并试图改变现有的窗口以满足我的需要。通过将AllowTransparency设置回false,我设法摆脱了两侧的泄漏。然后发生的问题是窗口顶部的边框,我通过将CaptionHeightWindowChrome设置为0来移除。

好的,泄漏消失了。但我相信我目前的问题与我上面写的内容有关。也就是说,我无法将宽度和高度绑定到某个东西,没有合适的属性。这是我从SystemParameters尝试的:

SystemParameters.FullPrimaryScreenHeight;
SystemParameters.FullPrimaryScreenWidth;
SystemParameters.MaximizedPrimaryScreenHeight;
SystemParameters.MaximizedPrimaryScreenWidth;
SystemParameters.PrimaryScreenHeight;
SystemParameters.PrimaryScreenWidth;
SystemParameters.WorkArea.Height;
SystemParameters.WorkArea.Width;
SystemParameters.VirtualScreenWidth;

所需的实际高度(1048)介于SystemParameters.WorkArea.Height(1040)和SystemParameters.MaximizedPrimaryScreenHeight(1056)之间。

并且所需的实际宽度(1928)介于SystemParameters.WorkArea.Width(1920)和SystemParameters.MaximizedPrimaryScreenWidth(1936)之间。

你可以在这里看到模式。

  • MaximizedPrimaryScreenWidth和MaximizedPrimaryScreenHeight都比我需要的大8个像素
  • WorkArea.Width和WorkArea.Height都比我需要的小8个像素
  • 我之前的所有方面都有8个像素的泄漏

这是我的窗口代码

<Window x:Class="MyApp.MainWindow"
        ...
        WindowState="Normal" 
        ResizeMode="CanResizeWithGrip"
        MaxHeight="{Binding SystemParameters.WorkArea.Height}"
        WindowStyle="None">
    <WindowChrome.WindowChrome>
        <WindowChrome CaptionHeight="0"/>
    </WindowChrome.WindowChrome>
    ....content....
</Window>

什么也行不通:

- Window.Padding
- Window.Margin
- Window.Top
- WindowStyle="SingleBorderWindow"

我有但不想要的

XAML:
MaxWidth="{Binding MaximumWidth}"

C#
public double MaximumWidth
{
    get
    {
        // I don't want anything hard-coded.
        // Some other (correct) parameter would be fine as well,
        // if we can't get the window on screen.
        return SystemParameters.WorkArea.Width + 8;
    }
}

更新:SingleBorderWindow

这是使用WindowStyle="SingleBorderWindow"时发生的情况。

enter image description here

所以,我的窗口不仅与我的右边和底边都缩短了8个像素(当绑定到WorkArea时),但你可以看到后面的实际窗口,按钮是可点击的(即使它们不可见)。真的很奇怪。这就像有两个窗口,但只有我需要的窗口不在正确的位置。

c# wpf xaml window fullscreen
2个回答
0
投票

所需的实际高度(1048)介于SystemParameters.WorkArea.Height(1040)和SystemParameters.MaximizedPrimaryScreenHeight(1056)之间。

并且所需的实际宽度(1928)介于SystemParameters.WorkArea.Width(1920)和SystemParameters.MaximizedPrimaryScreenWidth(1936)之间。

我认为你需要了解Windows Resolutions来帮助你解决这个问题

我相信15英寸窗口的分辨率宽度x高度为1366 X 768

因此,使用高于此分辨率的分辨率将使您的应用程序的大小超过默认系统分辨率。希望这有帮助


0
投票

哦。如果我猜好,在我们为您隐藏的代码中,在某个事件上,为了最大化您的窗口,您将其宽度更改为SystemParameters.MaximizedPrimaryScreenWidth。错误。绑定到WindowState并将其更改为WindowState.Maximized。

© www.soinside.com 2019 - 2024. All rights reserved.