0

いくつかのパラメーターを使用してウィンドウを作成しました。

<Window x:Class="MsgBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MsgBox" Height="300" Width="500" Topmost="True" WindowStartupLocation="CenterScreen" WindowStyle="None" Loaded="MsgBox_Loaded">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"></ColumnDefinition>
            <ColumnDefinition Width="2*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
    </Grid>
</Window>

高さと幅をこれらの計算された文字列に変更したいと思います。ユーザーの画面幅と高さを取得し、それを 4 で割ります。

Public ReadOnly Property PrimaryScreenWidth As Double
    Get
        Return System.Windows.SystemParameters.PrimaryScreenWidth
    End Get
End Property

Public ReadOnly Property PrimaryScreenHeight As Double
    Get
        Return System.Windows.SystemParameters.PrimaryScreenHeight
    End Get
End Property


Private MsgBoxWidth As String = PrimaryScreenWidth \ 4
Private MsgBoxHeight As String = PrimaryScreenHeight \ 4

ウィンドウに設定する方法は?

    Height="{x:static MsgBoxHeight }" Width="{x:static MsgBoxWidth }" ??
4

2 に答える 2

0

これをやりたいのなら、単純にやってみませんか

Me.Height = MsgBoxHeight
Me.Width = MsgBoxWidth

プロパティを計算したとき?

于 2013-03-10T14:46:49.283 に答える
0

中括弧を使用して、表示している、おそらく必要な構文:

Height="{x:static MsgBoxHeight }" Width="{x:static MsgBoxWidth }"

マークアップ拡張構文と呼ばれ、マークアップ拡張クラスを使用してプロパティ値を設定できます。これがあなたがそれをする方法です。最初のステップでは、マークアップ拡張クラスを作成します。

Public Class MsgBoxHeight
    Inherits System.Windows.Markup.MarkupExtension

    Public Sub New()
    End Sub

    Public Overrides Function ProvideValue(serviceProvider As IServiceProvider) As Object
        Return System.Windows.SystemParameters.PrimaryScreenHeight / 4
    End Function
End Class

次に、にを追加するxmlns:local="clr-namespace=YourNamespace"と、次の<Windows>ように使用できるようになりますHeight="{local:MsgBoxHeight}"

ウィンドウの完全なXAMLは次のようになります。

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication6"
    Title="MainWindow" Height="{local:MsgBoxHeight}" Width="525">
    <Grid>

    </Grid>
</Window>

ノート

マークアップ拡張機能を使用するということは、コードからそれを実行していることを意味します。XAMLに優れたアプリケーション固有の拡張機能を導入していますが、それを機能させるにはコードが必要です。1つのウィンドウに対してのみこれを行う必要がある場合は、マークアップ拡張機能を気にせずに、コードビハインドからウィンドウのHeightとを設定する方が理にかなっています。Width

于 2013-03-10T15:17:21.680 に答える