Como: Definir as propriedades Height de um Element
Exemplo
Este exemplo mostra visualmente as diferenças de comportamento de renderização entre as quatro propriedades relacionadas à altura do Windows Presentation Foundation (WPF).
A classe FrameworkElement expõe quatro propriedades que descrevem as características de altura de um elemento. Essas quatro propriedades podem entrar em conflito e quando isso acontece, o valor que tem prioridade é determinado sistema autônomo segue: the MinHeight valor prevalece sobre o MaxHeight valor, que por sua vez tem precedência sobre o Height valor. A quarta propriedade, ActualHeight, é somente leitura.
O exemplo de Extensible Application Markup Language (XAML) a seguir desenha um elemento Rectangle (rect1) como um filho de Canvas. Você pode alterar as propriedades de altura de um Rectangle usando uma série de ListBoxes que representam os valores das propriedade MinHeight, MaxHeight e Height. Dessa forma, a precedência de cada propriedade é exibida visualmente.
<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>
Os exemplos de code-behind a seguir manipulam os eventos que o evento SelectionChanged dispara. Cada método personalizado recebe a entrada do ListBox, analisa o valor como um Double e aplica o valor à propriedade de altura especificada. Os valores de altura também são convertidos em uma sequência de caracteres e escritos em um elemento TextBlock chamado txt1.
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;
}
For the complete sample, see Exemplo Propriedades Altura.
Consulte também
Tarefas
Como: Set the Width Properties of an Element