wpf 如何从xaml获取屏幕尺寸?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17416561/
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-13 09:07:18  来源:igfitidea点击:

How to obtain screen size from xaml?

c#wpf

提问by TTGroup

I'm using wpf on C# to design GUI, and I want to get screen size (The value of Width and Height) from xaml code.

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

I knew how to get them from C# code as

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

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

But I don't know how to get them from XAMLcode.

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

回答by Vlad Bezden

This will work. You can read more hereabout SystemParameters

这将起作用。您可以在此处阅读有关 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>

回答by Dominik Weber

Check out the SystemParameters class: http://msdn.microsoft.com/de-de/library/system.windows.systemparameters.fullprimaryscreenheight.aspx

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

You can use it like this:

你可以这样使用它:

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

(You could also use the x:Static notation to query the Windows Forms properties, however if you're developing a WPF application the SystemParameters class might be the way to go)

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

回答by Arushi Agrawal

Say your parent container in the XAML is grid, name it as grdForm

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

In the code behind can get the dimensions as

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

double width=grdForm.ActualWidth

双倍宽度=grdForm.ActualWidth

double height=grdForm.ActualHeight

双倍高度=grdForm.ActualHeight