【问题标题】:How to obtain screen size from xaml?如何从 xaml 获取屏幕尺寸?
【发布时间】:2013-07-02 12:10:04
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    这会奏效。您可以阅读更多关于 SystemParameters 的here

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

    【讨论】:

      【解决方案2】:

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

      你可以这样使用它:

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

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

      【讨论】:

        【解决方案3】:

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

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

        双倍宽度=grdForm.ActualWidth

        双倍高度=grdForm.ActualHeight

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多