Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
Esempio
Questo esempio mostra visivamente le differenze nel comportamento di rendering tra le quattro proprietà correlate alla larghezza in Windows Presentation Foundation (WPF).
La classe FrameworkElement espone quattro proprietà che descrivono le caratteristiche di larghezza di un elemento. Queste quattro proprietà possono essere in conflitto e, quando lo fanno, il valore che ha la precedenza viene determinato nel modo seguente: il valore MinWidth ha la precedenza sul valore MaxWidth, che a sua volta ha la precedenza sul valore Width. Una quarta proprietà, ActualWidth, è di sola lettura e segnala la larghezza effettiva come determinata dalle interazioni con il processo di layout.
Negli esempi XAML (Extensible Application Markup Language) seguenti viene disegnato un elemento Rectangle (rect1) come elemento figlio di Canvas. È possibile modificare le proprietà di larghezza di un Rectangle usando una serie di elementi ListBox che rappresentano i valori delle proprietà di MinWidth, MaxWidthe Width. In questo modo, la precedenza di ogni proprietà viene visualizzata visivamente.
<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>
Gli esempi di code-behind seguenti gestiscono gli eventi generati dall'evento SelectionChanged. Ciascun metodo personalizzato accetta l'input dal ListBox, interpreta il valore come Doublee applica il valore alla proprietà specificata relativa alla larghezza. I valori di larghezza vengono convertiti anche in una stringa e scritti in vari elementi TextBlock (la definizione di tali elementi non viene visualizzata nel codice XAML selezionato).
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
Per l'esempio completo, vedere esempio di confronto delle proprietà di larghezza.
Vedere anche
.NET Desktop feedback