方法 : 要素の Height プロパティを設定する
更新 : 2007 年 11 月
使用例
この例では、Windows Presentation Foundation (WPF) の高さに関連する 4 つのプロパティについて、レンダリング動作の違いを視覚的に示します。
FrameworkElement クラスは、要素の高さの特性を記述する 4 つのプロパティを公開します。これらの 4 つのプロパティは競合する可能性がありますが、その場合は、MinHeight 値、MaxHeight 値、Height 値の順序で優先して使用されます。4 つ目のプロパティ ActualHeight は読み取り専用です。
次の Extensible Application Markup Language (XAML) の例では、Rectangle 要素 (rect1) を Canvas の子として描画します。Rectangle の高さのプロパティは、MinHeight、MaxHeight、および Height の各プロパティ値を表す一連の ListBox を使用して変更できます。この方法では、各プロパティの優先順位が表示されます。
<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 として解析し、指定された高さに関連するプロパティに値を適用します。高さの値は文字列にも変換され、txt1 という名前の TextBlock 要素に書き込まれます。
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
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;
}
サンプル全体については、「Height プロパティのサンプル」を参照してください。