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 という名前を付けます。

コードビハインドでは、寸法を次のように取得できます

double width=grdForm.ActualWidth

double height=grdForm.ActualHeight

于 2013-07-02T03:48:41.250 に答える