如何获取 WPF 窗口的 ClientSize?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/955592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:31:57  来源:igfitidea点击:

How to get a WPF window's ClientSize?

wpfsize

提问by Joe White

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.

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

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 中没有看到任何类似的东西:没有 ClientSize、ClientWidth、ClientHeight、GetClientSize() 或其他任何我能猜到的名称。

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

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

采纳答案by CodeMonkey1313

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

你可以做到的一种方法是获取最顶层的子元素,转换this.Content为它的类型,然后调用.RenderSize它,这会给你它的大小。

<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

edit:

编辑:

as Trent said, ActualWidthand ActualHeightare also viable solutions. Basically easier methods of getting what I put above.

作为特伦特说,ActualWidthActualHeight也是可行的解决方案。基本上更简单的方法来获得我上面的内容。

回答by Adam D

var h = ((Panel)Application.Current.MainWindow.Content).ActualHeight;
var w = ((Panel)Application.Current.MainWindow.Content).ActualWidth;

回答by Trent F Guidry

One way to do it is with the code below. XAML:

一种方法是使用下面的代码。XAML:

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
    <Canvas>
    </Canvas>
</Window>

C#:

C#:

using System.Windows;

using System.IO;
using System.Xml;
using System.Windows.Controls;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            double dWidth = -1;
            double dHeight = -1;
            FrameworkElement pnlClient = this.Content as FrameworkElement;
            if (pnlClient != null)
            {
                dWidth = pnlClient.ActualWidth;
                dHeight = pnlClient.ActualHeight;
            }
        }
    }
}

回答by Gerard

I used a Gridwith VerticalAlignment=Top. As a result the Grid unfortunately didn't fill the parent Window anymore (which is its default behaviour, but the VerticalAligment property spoils it).

我用了一个GridVerticalAlignment=Top。结果不幸的是,Grid 不再填充父窗口(这是它的默认行为,但 VerticalAligment 属性破坏了它)。

I solved it by putting an empty Borderaround the Grid. This border fills the complete content of the window, it has the same dimensions as the default border that a wpf window has anyways.

我通过Border在网格周围放置一个空来解决它。该边框填充了窗口的完整内容,它与 wpf 窗口具有的默认边框具有相同的尺寸。

To get the Grid to fill the main window, I used the binding:
<Border BorderThickness="0" x:Name=Main> <Grid VerticalAlignment="Top" Height="{Binding ElementName=Main, Path=ActualHeight}"> ... </Grid> </Border>

为了让网格填充主窗口,我使用了绑定:
<Border BorderThickness="0" x:Name=Main> <Grid VerticalAlignment="Top" Height="{Binding ElementName=Main, Path=ActualHeight}"> ... </Grid> </Border>