方法: 要素の Height プロパティを設定する
例
この例では、Windows Presentation Foundation (WPF) の 4 つの高さに関連するプロパティのレンダリング動作の違いを視覚的に示します。
FrameworkElement クラスは、要素の高さの特性を記述する 4 つのプロパティを公開します。 これら 4 つのプロパティは競合する可能性があり、競合する場合、優先順位の高い値は次のように決定されます。MinHeight 値は MaxHeight 値よりも優先され、Height 値よりも優先されます。 4 番目のプロパティ ActualHeightは読み取り専用で、レイアウト プロセスとの対話によって決定される実際の高さを報告します。
次の拡張アプリケーション マークアップ言語 (XAML) の例では、Canvasの子として Rectangle 要素 (rect1
) を描画します。 MinHeight、MaxHeight、および Heightのプロパティ値を表す一連の ListBox 要素を使用して、Rectangle の高さのプロパティを変更できます。 この方法では、各プロパティの優先順位が視覚的に表示されます。
<Canvas Height="200" MinWidth="200" Background="#b0c4de" VerticalAlignment="Top" HorizontalAlignment="Center" Name="myCanvas" Margin="0,0,0,50">
<Rectangle HorizontalAlignment="Center" Canvas.Top="50" Canvas.Left="50" Name="rect1" Fill="#4682b4" Height="100" Width="100"/>
</Canvas>
<TextBlock Grid.Row="1" Grid.Column="0" Margin="10,0,0,0" TextWrapping="Wrap">Set the Rectangle Height:</TextBlock>
<ListBox Grid.Column="1" Grid.Row="1" Margin="10,0,0,0" Height="50" Width="50" SelectionChanged="changeHeight">
<ListBoxItem>25</ListBoxItem>
<ListBoxItem>50</ListBoxItem>
<ListBoxItem>75</ListBoxItem>
<ListBoxItem>100</ListBoxItem>
<ListBoxItem>125</ListBoxItem>
<ListBoxItem>150</ListBoxItem>
<ListBoxItem>175</ListBoxItem>
<ListBoxItem>200</ListBoxItem>
</ListBox>
<TextBlock Grid.Row="1" Grid.Column="2" Margin="10,0,0,0" TextWrapping="Wrap">Set the Rectangle MinHeight:</TextBlock>
<ListBox Grid.Column="3" Grid.Row="1" Margin="10,0,0,0" Height="50" Width="50" SelectionChanged="changeMinHeight">
<ListBoxItem>25</ListBoxItem>
<ListBoxItem>50</ListBoxItem>
<ListBoxItem>75</ListBoxItem>
<ListBoxItem>100</ListBoxItem>
<ListBoxItem>125</ListBoxItem>
<ListBoxItem>150</ListBoxItem>
<ListBoxItem>175</ListBoxItem>
<ListBoxItem>200</ListBoxItem>
</ListBox>
<TextBlock Grid.Row="1" Grid.Column="4" Margin="10,0,0,0" TextWrapping="Wrap">Set the Rectangle MaxHeight:</TextBlock>
<ListBox Grid.Column="5" Grid.Row="1" Margin="10,0,0,0" Height="50" Width="50" SelectionChanged="changeMaxHeight">
<ListBoxItem>25</ListBoxItem>
<ListBoxItem>50</ListBoxItem>
<ListBoxItem>75</ListBoxItem>
<ListBoxItem>100</ListBoxItem>
<ListBoxItem>125</ListBoxItem>
<ListBoxItem>150</ListBoxItem>
<ListBoxItem>175</ListBoxItem>
<ListBoxItem>200</ListBoxItem>
</ListBox>
次の分離コードの例では、SelectionChanged イベントによって発生するイベントを処理します。 各ハンドラーは、ListBoxから入力を受け取り、値を Doubleとして解析し、指定された高さに関連するプロパティに値を適用します。 高さの値も文字列に変換され、さまざまな TextBlock 要素に書き込まれます (これらの要素の定義は、選択した XAML には表示されません)。
private void changeHeight(object sender, SelectionChangedEventArgs args)
{
ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem);
Double sz1 = Double.Parse(li.Content.ToString());
rect1.Height = sz1;
rect1.UpdateLayout();
txt1.Text= "ActualHeight is set to " + rect1.ActualHeight;
txt2.Text= "Height is set to " + rect1.Height;
txt3.Text= "MinHeight is set to " + rect1.MinHeight;
txt4.Text= "MaxHeight is set to " + rect1.MaxHeight;
}
private void changeMinHeight(object sender, SelectionChangedEventArgs args)
{
ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem);
Double sz1 = Double.Parse(li.Content.ToString());
rect1.MinHeight = sz1;
rect1.UpdateLayout();
txt1.Text= "ActualHeight is set to " + rect1.ActualHeight;
txt2.Text= "Height is set to " + rect1.Height;
txt3.Text= "MinHeight is set to " + rect1.MinHeight;
txt4.Text= "MaxHeight is set to " + rect1.MaxHeight;
}
private void changeMaxHeight(object sender, SelectionChangedEventArgs args)
{
ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem);
Double sz1 = Double.Parse(li.Content.ToString());
rect1.MaxHeight = sz1;
rect1.UpdateLayout();
txt1.Text= "ActualHeight is set to " + rect1.ActualHeight;
txt2.Text= "Height is set to " + rect1.Height;
txt3.Text= "MinHeight is set to " + rect1.MinHeight;
txt4.Text= "MaxHeight is set to " + rect1.MaxHeight;
}
Private Sub changeHeight(ByVal sender As Object, ByVal args As SelectionChangedEventArgs)
Dim li As ListBoxItem = CType(CType(sender, ListBox).SelectedItem, ListBoxItem)
Dim sz1 As Double = Double.Parse(li.Content.ToString())
rect1.Height = sz1
rect1.UpdateLayout()
txt1.Text = "ActualHeight is set to " + rect1.ActualHeight.ToString
txt2.Text = "Height is set to " + rect1.Height.ToString
txt3.Text = "MinHeight is set to " + rect1.MinHeight.ToString
txt4.Text = "MaxHeight is set to " + rect1.MaxHeight.ToString
End Sub
Private Sub changeMinHeight(ByVal sender As Object, ByVal args As SelectionChangedEventArgs)
Dim li As ListBoxItem = CType(CType(sender, ListBox).SelectedItem, ListBoxItem)
Dim sz1 As Double = Double.Parse(li.Content.ToString())
rect1.MinHeight = sz1
rect1.UpdateLayout()
txt1.Text = "ActualHeight is set to " + rect1.ActualHeight.ToString
txt2.Text = "Height is set to " + rect1.Height.ToString
txt3.Text = "MinHeight is set to " + rect1.MinHeight.ToString
txt4.Text = "MaxHeight is set to " + rect1.MaxHeight.ToString
End Sub
Private Sub changeMaxHeight(ByVal sender As Object, ByVal args As SelectionChangedEventArgs)
Dim li As ListBoxItem = CType(CType(sender, ListBox).SelectedItem, ListBoxItem)
Dim sz1 As Double = Double.Parse(li.Content.ToString())
rect1.MaxHeight = sz1
rect1.UpdateLayout()
txt1.Text = "ActualHeight is set to " + rect1.ActualHeight.ToString
txt2.Text = "Height is set to " + rect1.Height.ToString
txt3.Text = "MinHeight is set to " + rect1.MinHeight.ToString
txt4.Text = "MaxHeight is set to " + rect1.MaxHeight.ToString
End Sub
完全なサンプルについては、「Height Properties Sample」を参照してください。
関連項目
- FrameworkElement
- ListBox
- ActualHeight
- MaxHeight
- MinHeight
- Height
- 要素 の Width プロパティを設定します。
- パネルの概要
- 高さプロパティのサンプル
.NET Desktop feedback