ARTICLE

Working with Brushes in WPF: Part 6

Posted by Manish Tewatia Articles | WPF using VB.NET September 15, 2010
In this article you learn about the all types of brushes and how to working with them in WPF.
 
Reader Level:

Brushes: As you learn in my previous article part 1, part 2, part 3, part 4, part 5 a brush is used to describe the background of a button, the foreground of text, and the fill of a shape and all brushes classes are inherit from System.Windows.Media.Brush and give you more exotic effects.

Here we discuss about the third Brush class that is VisualBrush.

VisualBrush: A VisualBrush paints an area with a Visual. It is an unusual brush that allows you to take the visual content of an element and use it to fill any surface. Like using a VisualBrush you could copy the appearance of a button in a window to a region somewhere else in that same window. However, the button copy won't be clickable or interactive in any way. A VisualBrush also enables you to project content from one portion of your application into another area; it's very useful for creating reflection effects and magnifying portions of the screen.

Example of an VisualBrush
Xaml Code

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width
="525">
    <Rectangle Width="150" Height="150">
        <Rectangle.Fill>
            <VisualBrush TileMode="Tile">
                <VisualBrush.Visual>
                    <StackPanel>
                        <StackPanel.Background>
                            <DrawingBrush>
                                <DrawingBrush.Drawing>
                                    <GeometryDrawing>
                                        <GeometryDrawing.Brush>
                                            <RadialGradientBrush>
                                                <GradientStop Color="Blue" Offset="0.1" />
                                                <GradientStop Color="Green" Offset="1.0" />
                                            </RadialGradientBrush>
                                        </GeometryDrawing.Brush>
                                        <GeometryDrawing.Geometry>
                                            <GeometryGroup>
                                                <RectangleGeometry Rect="0,0,50,50" />
                                                <RectangleGeometry Rect="50,50,50,50" />
                                            </GeometryGroup>
                                        </GeometryDrawing.Geometry>
                                    </GeometryDrawing>
                                </DrawingBrush.Drawing>
                            </DrawingBrush>
                        </StackPanel.Background>
                        <TextBlock FontSize="10pt" Margin="10">MANISH</TextBlock>
                    </StackPanel>
                </VisualBrush.Visual>
            </VisualBrush>
        </Rectangle.Fill>
    </Rectangle>
</
Window>

VB Code

    Dim Rectangle6 As New Rectangle()
        Rectangle6.Width = 150
        Rectangle6.Height = 150
    ' Create a VisualBrush and use it
    ' to paint the rectangle.
    Dim Brush As New VisualBrush() 
    '
    ' Create the brush's contents.
    '
    Dim Panel As New StackPanel()
    ' Create a DrawingBrush and use it to
    ' paint the panel.
    Dim myBrush As New DrawingBrush()
    Dim myGroup As New GeometryGroup()
        myGroup.Children.Add(New RectangleGeometry(New Rect(0, 0, 50, 50)))
        myGroup.Children.Add(New RectangleGeometry(New Rect(50, 50, 50, 50)))
    Dim checkerBrush As New RadialGradientBrush()
        checkerBrush.GradientStops.Add(New GradientStop(Colors.Blue, 0.1))
        checkerBrush.GradientStops.Add(New GradientStop(Colors.green, 1.0))
    Dim checkers As New GeometryDrawing(checkerBrush, Nothing, myGroup)
        myBrush.Drawing = checkers
        aPanel.Background = myDrawingBrushBrush
    ' Create some text.
    Dim someText As New TextBlock()
        someText.Text = "Hello, World"
    Dim fSizeConverter As New FontSizeConverter()
        someText.FontSize = CDbl(fSizeConverter.ConvertFromString("10pt"))
        someText.Margin = New Thickness(10)
        aPanel.Children.Add(someText) 
        myBrush.Visual = aPanel
        Rectangle6.Fill = myBrush

Output Window

brush6.gif

Conclusion

Hope this article will help you understand the working of VisualBrush in WPF.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor