WPF窗口最大化宽度和高度 [英] WPF window maximized width and height

查看:176
本文介绍了WPF窗口最大化宽度和高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR

似乎将 AllowTransparency 设置为 True 可使窗口在最大化时向上和向左偏移8个像素,我需要将其重新显示在屏幕上.

It seems setting the AllowTransparency to True offsets the window up and left by 8 pixels when maximized, I need to get it back on screen.

深入

似乎我在某个地方犯了一个错误,但不确定为什么.我无法发布完整的代码,但是我将尝试在此处编写所有相关内容.

It looks like I am making a mistake somewhere, but am not sure why. I cannot post the entire code, but I will try to write everything relevant here.

我有我的主窗口,其中有一个自定义标题栏.这意味着我必须将 WindowsStyle 设置为 None ,并且我也在使用模板.从一开始,我就遇到了问题,即窗口要比桌面大得多,而窗口又要最大化.在所有屏幕上,屏幕偏离了8个像素.我放弃了模板,并尝试更改现有窗口以适合我的需求.通过将 AllowTransparency 设置回 false ,我设法摆脱了两侧的泄漏.然后发生的问题是窗口顶部的边框,我通过将 WindowChrome CaptionHeight 设置为0来删除了该边框.

I have my main window, which has a custom title bar. That means I had to set WindowsStyle to None and I was also using a template. Right from the start, I had problems with window being much bigger than my desktop, while maximized. It was going 8 pixels off screen on ALL SIDES. I ditched the template and tried to alter the existing window to suit my needs. By setting AllowTransparency back to false, I managed to get rid of the leakage on the sides. Problem that then occurred, is the border on top of the window, which I removed by setting the CaptionHeight of WindowChrome to 0.

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

Okay, the leakage is gone. But I believe my current problem is connected with what I wrote above. That is, I can't bind width and height to something, there is no appropriate property. This is what I tried from 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)之间.

The actual height required (1048) is somewhere between SystemParameters.WorkArea.Height (1040) and SystemParameters.MaximizedPrimaryScreenHeight (1056).

所需的实际宽度(1928)在 SystemParameters.WorkArea.Width (1920)和 SystemParameters.MaximizedPrimaryScreenWidth (1936)之间.

And the actual width required (1928) is somewhere between SystemParameters.WorkArea.Width (1920) and SystemParameters.MaximizedPrimaryScreenWidth (1936).

您可以在这里看到图案.

You can see the pattern here.

  • 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" 时发生的情况.

This is what happens when using WindowStyle="SingleBorderWindow".

因此,我的窗口不仅左右边缘都短了8个像素(绑定到WorkArea时),而且您可以看到后面的实际窗口,并且这些按钮是可单击的(即使它们不可见).真的很奇怪.就像有两个窗口,但是只有我需要的一个窗口不在正确的位置.

So, not only is my window 8 pixels short to both my right and bottom edges (while binding to WorkArea), but you can see the actual window behind and the buttons are clickable (even where they are not visible). Really weird. It is like there are two windows, but only the one I need is not on the right location.

推荐答案

哦.如果我猜对了,在某些情况下,为了使您的窗口最大化,可以在对我们隐藏的代码中,将其宽度更改为SystemParameters.MaximizedPrimaryScreenWidth.错误的.绑定到WindowState并将其更改为WindowState.Maximized.

Oh. If I guess well, in your code which is hidden for us, on a certain event, to maximize your window, you change it's width to SystemParameters.MaximizedPrimaryScreenWidth. Wrong. Bind to WindowState and change it to WindowState.Maximized.

这篇关于WPF窗口最大化宽度和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆