10

我在 C# 上使用 wpf 来设计 GUI,我想从 xaml 代码中获取屏幕大小(宽度和高度的值)。

我知道如何从 C# 代码中获取它们

   Width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
   Height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;

但我不知道如何从XAML代码中获取它们。

4

3 回答 3

22

这将起作用。您可以在此处阅读有关 SystemParameters的更多信息

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TextBlock Text="{Binding Source={x:Static SystemParameters.FullPrimaryScreenHeight}}" />
        <TextBlock Text="{Binding Source={x:Static SystemParameters.FullPrimaryScreenWidth}}" />
        <TextBlock Text="{Binding Source={x:Static SystemParameters.PrimaryScreenHeight}}" />
        <TextBlock Text="{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}}" />
    </StackPanel>
</Window>
于 2013-07-02T03:08:59.820 回答
10

查看 SystemParameters 类:http: //msdn.microsoft.com/de-de/library/system.windows.systemparameters.fullprimaryscreenheight.aspx

你可以像这样使用它:

<Object Attribute="{x:Static SystemParameters.FullPrimaryScreenHeight}"/>

(您也可以使用 x:Static 表示法来查询 Windows 窗体属性,但是如果您正在开发 WPF 应用程序,则 SystemParameters 类可能是要走的路)

于 2013-07-02T02:44:51.653 回答
2

假设您在 XAML 中的父容器是网格,将其命名为 grdForm

在后面的代码中可以得到尺寸为

双倍宽度=grdForm.ActualWidth

双倍高度=grdForm.ActualHeight

于 2013-07-02T03:48:41.250 回答