Paints an area with a Visual.
Assembly: PresentationCore (in PresentationCore.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
There are two ways to specify the Visual content of a .
When you define a new Visual for a and that Visual is a UIElement (such as a panel or control), the layout system runs on the UIElement and its child elements when the AutoLayoutContent property is set to true. However, the root UIElement is essentially isolated from the rest of the system; styles, storyboards, and external layout can't permeate this boundary. Therefore, you should explicitly specify the size of the root UIElement, because its only parent is the and therefore it cannot automatically size itself to the area being painted. For more information about layout in Windows Presentation Foundation (WPF), see the The Layout System.
Updates to the visual tree of a will not propagate if a BitmapEffect is in the brush's parent chain. You can workaround this limitation by forcing an update of the scene on the object above the effect. You can call InvalidateVisual or include an animation to force a scene update.
Freezable Features: Because it inherits from the Freezable class, the class provides several special features: objects can be declared as resources and shared among multiple objects. For more information about the different features provided by Freezable objects, see the Freezable Objects Overview.
A cannot be made read-only (frozen) when its Visual property is set to any value other than null.
This example shows how to use the class to paint an area with a Visual.
In the following example, several controls and a panel are used as the background of a rectangle.
<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>
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;
For more information about and additional examples, see the Painting with Images, Drawings, and Visuals overview.
This code example is part of a larger example provided for the class. For the complete sample, see the VisualBrush Sample.
|How to: Create a Reflection||This example shows how to use a to create a reflection. Because a can display an existing visual, you can use this capability to produce interesting visual effects, such as reflections and magnification.|
|How to: Paint an Area with a Video||This example shows how to paint an area with media. One way to paint an area with media is to use a MediaElement together with a . Use the MediaElement to load and play the media, and then use it to set the Visual property of the . You can then use the to paint an area with the loaded media.|
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.