如何获取 WPF 窗口的 ClientSize? [英] How to get a WPF window's ClientSize?

查看:27
本文介绍了如何获取 WPF 窗口的 ClientSize?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WinForms 中,Form 有一个 ClientSize 属性(继承自 Control),它返回其客户区的大小,即标题栏和窗口边框内的区域.

In WinForms, Form had a ClientSize property (inherited from Control), which returns the size of its client area, i.e., the area inside the title bar and window borders.

我在 WPF 中没有看到任何类似的东西:没有 ClientSize、ClientWidth、ClientHeight、GetClientSize() 或任何我能想到的名字.

I'm not seeing anything similar in WPF: there's no ClientSize, ClientWidth, ClientHeight, GetClientSize(), or anything else that I can think to guess the name of.

如何获取 WPF 窗口的客户端大小?

How do I go about getting the client size of a WPF Window?

推荐答案

一种方法是获取最顶层的子元素,将 this.Content 转换为其类型,然后调用 .RenderSize 在它上面,它会给你它的大小.

One way you could do it is to take the top most child element, cast this.Content to its type, and call .RenderSize on it, which will give you its size.

<Window x:Class="XML_Reader.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="400" Width="600" WindowStyle="SingleBorderWindow">
    <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
    </Grid>
</Window>

((Grid)this.Content).RenderSize.Height
((Grid)this.Content).RenderSize.Width

正如 Trent 所说,ActualWidthActualHeight 也是可行的解决方案.基本上更容易获得我上面提到的内容.

as Trent said, ActualWidth and ActualHeight are also viable solutions. Basically easier methods of getting what I put above.

这篇关于如何获取 WPF 窗口的 ClientSize?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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