Condividi tramite


Procedura: disegnare un'area con un oggetto Visual

In questo esempio viene illustrato come utilizzare la classe VisualBrush per disegnare un'area con Visual.

Nell'esempio riportato di seguito vengono utilizzati numerosi controlli e un pannello come sfondo di un rettangolo.

Esempio

<Rectangle Width="150" Height="150" Stroke="Black" Margin="5,0,5,0">
  <Rectangle.Fill>
    <VisualBrush>
      <VisualBrush.Visual>
        <StackPanel Background="White">
          <Rectangle Width="25" Height="25" Fill="Red" Margin="2" />
          <TextBlock FontSize="10pt" Margin="2">Hello, World!</TextBlock>
          <Button Margin="2">A Button</Button>
        </StackPanel>
      </VisualBrush.Visual>
    </VisualBrush>
  </Rectangle.Fill>
</Rectangle>
            Dim myVisualBrush As New VisualBrush()

            ' Create the visual brush's contents.
            Dim myStackPanel As New StackPanel()
            myStackPanel.Background = Brushes.White

            Dim redRectangle As New Rectangle()
            With redRectangle
                .Width = 25
                .Height = 25
                .Fill = Brushes.Red
                .Margin = New Thickness(2)
            End With
            myStackPanel.Children.Add(redRectangle)

            Dim someText As New TextBlock()
            Dim myFontSizeConverter As New FontSizeConverter()
            someText.FontSize = CDbl(myFontSizeConverter.ConvertFrom("10pt"))
            someText.Text = "Hello, World!"
            someText.Margin = New Thickness(2)
            myStackPanel.Children.Add(someText)

            Dim aButton As New Button()
            aButton.Content = "A Button"
            aButton.Margin = New Thickness(2)
            myStackPanel.Children.Add(aButton)

            ' Use myStackPanel as myVisualBrush's content.
            myVisualBrush.Visual = myStackPanel

            ' Create a rectangle to paint.
            Dim myRectangle As New Rectangle()
            With myRectangle
                .Width = 150
                .Height = 150
                .Stroke = Brushes.Black
                .Margin = New Thickness(5, 0, 5, 0)
            End With

            ' Use myVisualBrush to paint myRectangle.
            myRectangle.Fill = myVisualBrush

            VisualBrush myVisualBrush = new VisualBrush();

            // Create the visual brush's contents.
            StackPanel myStackPanel = new StackPanel();
            myStackPanel.Background = Brushes.White;

            Rectangle redRectangle = new Rectangle();
            redRectangle.Width = 25;
            redRectangle.Height =25; 
            redRectangle.Fill = Brushes.Red;
            redRectangle.Margin = new Thickness(2);
            myStackPanel.Children.Add(redRectangle);

            TextBlock someText = new TextBlock();
            FontSizeConverter myFontSizeConverter = new FontSizeConverter();
            someText.FontSize = (double)myFontSizeConverter.ConvertFrom("10pt");
            someText.Text = "Hello, World!";
            someText.Margin = new Thickness(2);
            myStackPanel.Children.Add(someText);

            Button aButton = new Button();
            aButton.Content = "A Button";
            aButton.Margin = new Thickness(2);
            myStackPanel.Children.Add(aButton);

            // Use myStackPanel as myVisualBrush's content.
            myVisualBrush.Visual = myStackPanel;

            // Create a rectangle to paint.
            Rectangle myRectangle = new Rectangle();
            myRectangle.Width = 150;
            myRectangle.Height = 150;
            myRectangle.Stroke = Brushes.Black;
            myRectangle.Margin = new Thickness(5,0,5,0);

            // Use myVisualBrush to paint myRectangle.
            myRectangle.Fill = myVisualBrush;

Per ulteriori informazioni su VisualBrush e per esempi aggiuntivi, vedere i Cenni preliminari su Disegnare con oggetti Image, Drawing e Visual.

Questo esempio di codice fa parte di un esempio più esaustivo fornito per la classe VisualBrush. Per l'esempio completo, vedere Esempio VisualBrush (la pagina potrebbe essere in inglese).

Vedere anche

Concetti

Disegnare con oggetti Image, Drawing e Visual