如何:设置元素的高度属性

示例:

此示例直观地显示了 Windows Presentation Foundation (WPF) 中四个高度相关的属性之间的呈现行为差异。

FrameworkElement 类公开四个描述元素高度特征的属性。 这四个属性可能会发生冲突,当它们发生冲突时,优先值将按如下方式确定:MinHeight 值优先于 MaxHeight 值,而值又优先于 Height 值。 第四个属性 ActualHeight 是只读的,并报告通过与布局过程的交互确定的实际高度。

以下可扩展应用程序标记语言(XAML)示例将 Rectangle 元素(rect1)绘制为 Canvas子级。 可以通过使用一系列表示RectangleListBoxMinHeight属性值的MaxHeight元素,更改Height的高度属性。 通过这种方式,每个属性的优先级会以可视化形式显示。

<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获取输入,将值分析为 a 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

有关完整示例,请参阅 高度属性示例

另请参阅