Freigeben über


Gewusst wie: Anwenden von Stretch-Eigenschaften auf den Inhalt einer Viewbox

Beispiel

In diesem Beispiel wird veranschaulicht, wie der Wert einer StretchDirection-Eigenschaft und einer Stretch-Eigenschaft einer Viewbox geändert wird.

Im ersten Beispiel wird Extensible Application Markup Language (XAML) verwendet, um ein Viewbox-Element zu definieren. Eine MaxWidth-Eigenschaft und eine MaxHeight-Eigenschaft von 400 werden zugewiesen. In diesem Beispiel wird ein Image-Element innerhalb des Viewbox geschachtelt. Button-Elemente, die den Eigenschaftswerten der Stretch-Enumeration und der StretchDirection-Enumeration entsprechen, ändern das Streckverhalten des geschachtelten Image.

<StackPanel Margin="0,0,0,10" HorizontalAlignment="Center" Orientation="Horizontal" DockPanel.Dock="Top">
    <Button Name="btn1" Click="stretchNone">Stretch="None"</Button>
    <Button Name="btn2" Click="stretchFill">Stretch="Fill"</Button>
    <Button Name="btn3" Click="stretchUni">Stretch="Uniform"</Button>
    <Button Name="btn4" Click="stretchUniFill">Stretch="UniformToFill"</Button>
</StackPanel>

<StackPanel Margin="0,0,0,10" HorizontalAlignment="Center" Orientation="Horizontal" DockPanel.Dock="Top">
    <Button Name="btn5" Click="sdUpOnly">StretchDirection="UpOnly"</Button>
    <Button Name="btn6" Click="sdDownOnly">StretchDirection="DownOnly"</Button>
    <Button Name="btn7" Click="sdBoth">StretchDirection="Both"</Button>
 </StackPanel>        

<TextBlock DockPanel.Dock="Top" Name="txt1" />
<TextBlock DockPanel.Dock="Top" Name="txt2" />

<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
    <Viewbox MaxWidth="500" MaxHeight="500" Name="vb1">
        <Image Source="tulip_farm.jpg"/>
    </Viewbox>    
</StackPanel>

Die folgende Code-Behind-Datei behandelt die ButtonClick-Ereignisse, die im vorherigen XAML-Beispiel definiert wurden.

Private Sub stretchNone(ByVal sender As Object, ByVal args As RoutedEventArgs)

    vb1.Stretch = System.Windows.Media.Stretch.None
    txt1.Text = "Stretch is now set to None."
End Sub

Private Sub stretchFill(ByVal sender As Object, ByVal args As RoutedEventArgs)

    vb1.Stretch = System.Windows.Media.Stretch.Fill
    txt1.Text = "Stretch is now set to Fill."
End Sub

Private Sub stretchUni(ByVal sender As Object, ByVal args As RoutedEventArgs)

    vb1.Stretch = System.Windows.Media.Stretch.Uniform
    txt1.Text = "Stretch is now set to Uniform."
End Sub

Private Sub stretchUniFill(ByVal sender As Object, ByVal args As RoutedEventArgs)

    vb1.Stretch = System.Windows.Media.Stretch.UniformToFill
    txt1.Text = "Stretch is now set to UniformToFill."
End Sub

Private Sub sdUpOnly(ByVal sender As Object, ByVal args As RoutedEventArgs)

    vb1.StretchDirection = System.Windows.Controls.StretchDirection.UpOnly
    txt2.Text = "StretchDirection is now UpOnly."
End Sub

Private Sub sdDownOnly(ByVal sender As Object, ByVal args As RoutedEventArgs)

    vb1.StretchDirection = System.Windows.Controls.StretchDirection.DownOnly
    txt2.Text = "StretchDirection is now DownOnly."
End Sub

Private Sub sdBoth(ByVal sender As Object, ByVal args As RoutedEventArgs)

    vb1.StretchDirection = System.Windows.Controls.StretchDirection.Both
    txt2.Text = "StretchDirection is now Both."
End Sub
private void stretchNone(object sender, RoutedEventArgs e)
{
    vb1.Stretch = System.Windows.Media.Stretch.None;
    txt1.Text = "Stretch is now set to None.";
}

private void stretchFill(object sender, RoutedEventArgs e)
{
    vb1.Stretch = System.Windows.Media.Stretch.Fill;
    txt1.Text = "Stretch is now set to Fill.";
}

private void stretchUni(object sender, RoutedEventArgs e)
{
    vb1.Stretch = System.Windows.Media.Stretch.Uniform;
    txt1.Text = "Stretch is now set to Uniform.";
}

private void stretchUniFill(object sender, RoutedEventArgs e)
{
    vb1.Stretch = System.Windows.Media.Stretch.UniformToFill;
    txt1.Text = "Stretch is now set to UniformToFill.";
}

private void sdUpOnly(object sender, RoutedEventArgs e)
{
    vb1.StretchDirection = System.Windows.Controls.StretchDirection.UpOnly;
    txt2.Text = "StretchDirection is now UpOnly.";
}

private void sdDownOnly(object sender, RoutedEventArgs e)
{
    vb1.StretchDirection = System.Windows.Controls.StretchDirection.DownOnly;
    txt2.Text = "StretchDirection is now DownOnly.";
}

private void sdBoth(object sender, RoutedEventArgs e)
{
    vb1.StretchDirection = System.Windows.Controls.StretchDirection.Both;
    txt2.Text = "StretchDirection is now Both.";
}

Siehe auch

Referenz

Viewbox

Stretch

StretchDirection