【问题标题】:How to get a WPF window's ClientSize?如何获取 WPF 窗口的 ClientSize?
【发布时间】:2009-06-05 12:25:21
【问题描述】:

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

我在 WPF 中没有看到任何类似的东西:没有 ClientSize、ClientWidth、ClientHeight、GetClientSize() 或其他任何我能猜到的名称。

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

【问题讨论】:

    标签: wpf size


    【解决方案1】:

    您可以这样做的一种方法是获取最顶层的子元素,将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
    

    编辑:

    正如 Trent 所说,ActualWidthActualHeight 也是可行的解决方案。基本上更简单的方法来获取我上面的内容。

    【讨论】:

    • 我还要考虑内容边距。
    【解决方案2】:
    var h = ((Panel)Application.Current.MainWindow.Content).ActualHeight;
    var w = ((Panel)Application.Current.MainWindow.Content).ActualWidth;
    

    【讨论】:

    • @WelshKing - Loaded 事件触发了吗?在窗口渲染之前不会有大小。
    【解决方案3】:

    一种方法是使用下面的代码。 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#:

    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;
                }
            }
        }
    }
    

    【讨论】:

      【解决方案4】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-11-19
        • 2017-12-05
        • 2012-05-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多