Not
Åtkomst till den här sidan kräver auktorisering. Du kan prova att logga in eller ändra kataloger.
Åtkomst till den här sidan kräver auktorisering. Du kan prova att ändra kataloger.
Exempel
Det här exemplet visar visuellt skillnaderna i återgivningsbeteende mellan de fyra breddrelaterade egenskaperna i Windows Presentation Foundation (WPF).
Klassen FrameworkElement exponerar fyra egenskaper som beskriver breddegenskaperna för ett element. Dessa fyra egenskaper kan vara i konflikt, och när de gör det bestäms det värde som har företräde på följande sätt: MinWidth-värdet har företräde framför det MaxWidth värdet, vilket i sin tur har företräde framför det Width värdet. En fjärde egenskap, ActualWidth, är skrivskyddad och rapporterar den faktiska bredden som bestäms av interaktioner med layoutprocessen.
Följande exempel i Extensible Application Markup Language (XAML) skapar ett Rectangle-element (rect1) som barn till Canvas. Du kan ändra breddegenskaperna för en Rectangle med hjälp av en serie ListBox element som representerar egenskapsvärdena för MinWidth, MaxWidthoch Width. På så sätt visas prioriteten för varje egenskap visuellt.
<Canvas Height="200" MinWidth="200" Background="#b0c4de" VerticalAlignment="Top" HorizontalAlignment="Center" Name="myCanvas">
<Rectangle HorizontalAlignment="Center" Canvas.Top="50" Canvas.Left="50" Name="rect1" Fill="#4682b4" Width="100" Height="100"/>
</Canvas>
<TextBlock Grid.Row="1" Grid.Column="0" Margin="10,0,0,0" TextWrapping="Wrap">Set the Rectangle Width:</TextBlock>
<ListBox Grid.Column="1" Grid.Row="1" Margin="10,0,0,0" Width="50" Height="50" SelectionChanged="changeWidth">
<ListBoxItem>25</ListBoxItem>
<ListBoxItem>50</ListBoxItem>
<ListBoxItem>75</ListBoxItem>
<ListBoxItem>100</ListBoxItem>
<ListBoxItem>125</ListBoxItem>
<ListBoxItem>150</ListBoxItem>
<ListBoxItem>175</ListBoxItem>
<ListBoxItem>200</ListBoxItem>
<ListBoxItem>225</ListBoxItem>
<ListBoxItem>250</ListBoxItem>
</ListBox>
<TextBlock Grid.Row="1" Grid.Column="2" Margin="10,0,0,0" TextWrapping="Wrap">Set the Rectangle MinWidth:</TextBlock>
<ListBox Grid.Column="3" Grid.Row="1" Margin="10,0,0,0" Width="50" Height="50" SelectionChanged="changeMinWidth">
<ListBoxItem>25</ListBoxItem>
<ListBoxItem>50</ListBoxItem>
<ListBoxItem>75</ListBoxItem>
<ListBoxItem>100</ListBoxItem>
<ListBoxItem>125</ListBoxItem>
<ListBoxItem>150</ListBoxItem>
<ListBoxItem>175</ListBoxItem>
<ListBoxItem>200</ListBoxItem>
<ListBoxItem>225</ListBoxItem>
<ListBoxItem>250</ListBoxItem>
</ListBox>
<TextBlock Grid.Row="1" Grid.Column="4" Margin="10,0,0,0" TextWrapping="Wrap">Set the Rectangle MaxWidth:</TextBlock>
<ListBox Grid.Column="5" Grid.Row="1" Margin="10,0,0,0" Width="50" Height="50" SelectionChanged="changeMaxWidth">
<ListBoxItem>25</ListBoxItem>
<ListBoxItem>50</ListBoxItem>
<ListBoxItem>75</ListBoxItem>
<ListBoxItem>100</ListBoxItem>
<ListBoxItem>125</ListBoxItem>
<ListBoxItem>150</ListBoxItem>
<ListBoxItem>175</ListBoxItem>
<ListBoxItem>200</ListBoxItem>
<ListBoxItem>225</ListBoxItem>
<ListBoxItem>250</ListBoxItem>
</ListBox>
Följande exempel på kod bakom hanterar de händelser som den SelectionChanged händelsen genererar. Varje anpassad metod tar indata från ListBox, parsar värdet som en Doubleoch tillämpar värdet på den angivna breddrelaterade egenskapen. Breddvärdena konverteras också till en sträng och skrivs till olika TextBlock element (definitionen av dessa element visas inte i den valda XAML).
private void changeWidth(object sender, SelectionChangedEventArgs args)
{
ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem);
Double sz1 = Double.Parse(li.Content.ToString());
rect1.Width = sz1;
rect1.UpdateLayout();
txt1.Text = "ActualWidth is set to " + rect1.ActualWidth;
txt2.Text = "Width is set to " + rect1.Width;
txt3.Text = "MinWidth is set to " + rect1.MinWidth;
txt4.Text = "MaxWidth is set to " + rect1.MaxWidth;
}
private void changeMinWidth(object sender, SelectionChangedEventArgs args)
{
ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem);
Double sz1 = Double.Parse(li.Content.ToString());
rect1.MinWidth = sz1;
rect1.UpdateLayout();
txt1.Text = "ActualWidth is set to " + rect1.ActualWidth;
txt2.Text = "Width is set to " + rect1.Width;
txt3.Text = "MinWidth is set to " + rect1.MinWidth;
txt4.Text = "MaxWidth is set to " + rect1.MaxWidth;
}
private void changeMaxWidth(object sender, SelectionChangedEventArgs args)
{
ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem);
Double sz1 = Double.Parse(li.Content.ToString());
rect1.MaxWidth = sz1;
rect1.UpdateLayout();
txt1.Text = "ActualWidth is set to " + rect1.ActualWidth;
txt2.Text = "Width is set to " + rect1.Width;
txt3.Text = "MinWidth is set to " + rect1.MinWidth;
txt4.Text = "MaxWidth is set to " + rect1.MaxWidth;
}
Private Sub changeWidth(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.Width = sz1
rect1.UpdateLayout()
txt1.Text = "ActualWidth is set to " + rect1.ActualWidth.ToString
txt2.Text = "Width is set to " + rect1.Width.ToString
txt3.Text = "MinWidth is set to " + rect1.MinWidth.ToString
txt4.Text = "MaxWidth is set to " + rect1.MaxWidth.ToString
End Sub
Private Sub changeMinWidth(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.MinWidth = sz1
rect1.UpdateLayout()
txt1.Text = "ActualWidth is set to " + rect1.ActualWidth.ToString
txt2.Text = "Width is set to " + rect1.Width.ToString
txt3.Text = "MinWidth is set to " + rect1.MinWidth.ToString
txt4.Text = "MaxWidth is set to " + rect1.MaxWidth.ToString
End Sub
Private Sub changeMaxWidth(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.MaxWidth = sz1
rect1.UpdateLayout()
txt1.Text = "ActualWidth is set to " + rect1.ActualWidth.ToString
txt2.Text = "Width is set to " + rect1.Width.ToString
txt3.Text = "MinWidth is set to " + rect1.MinWidth.ToString
txt4.Text = "MaxWidth is set to " + rect1.MaxWidth.ToString
End Sub
Det fullständiga exemplet finns i Width Properties Comparison Sample.
Se även
.NET Desktop feedback