VisualBrush Binding to a Button


   
     
<Window x:Class="Drawing.VisualBrush"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="VisualBrush" Height="300" Width="300"
    >
  <StackPanel Margin="3">
    <Button Name="cmd" Margin="3" Padding="5">Is this a real button?</Button>
    <Rectangle Margin="3" Height="{Binding ElementName=cmd,Path=ActualHeight}">
      <Rectangle.Fill>
        <VisualBrush Visual="{Binding ElementName=cmd}"></VisualBrush>
      </Rectangle.Fill>
    </Rectangle>
    <Rectangle Margin="3" Height="50">
      <Rectangle.Fill>
        <VisualBrush Visual="{Binding ElementName=cmd}"></VisualBrush>
      </Rectangle.Fill>
    </Rectangle>
    <Rectangle Margin="3" Height="150">
      <Rectangle.Fill>
        <VisualBrush Visual="{Binding ElementName=cmd}"></VisualBrush>
      </Rectangle.Fill>
    </Rectangle>
  </StackPanel>
</Window>

   
    
    
    
    
     


Tile VisualBrush


   
     
<Page  
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  x:Class="Microsoft.Samples.Graphics.UsingVisualBrush.PaintingWithVisuals">
    <StackPanel>
        <Rectangle Width="150" Height="150" Stroke="Black" Margin="0,0,5,0">
            <Rectangle.Fill>
                <VisualBrush Viewport="0,0,95,35" ViewportUnits="Absolute" TileMode="Tile">
                    <VisualBrush.Visual>
                        <StackPanel Background="Transparent">
                            <TextBlock FontSize="10pt" Margin="10">Hello, World!</TextBlock>
                        </StackPanel>
                    </VisualBrush.Visual>
                </VisualBrush>
            </Rectangle.Fill>
        </Rectangle>
    </StackPanel>
</Page>

   
    
    
    
    
     


Use VisualBrush to paint background


   
     

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  x:Class="Microsoft.Samples.Graphics.UsingVisualBrush.PaintingWithVisuals">
    <StackPanel>
        <StackPanel.Background>
            <VisualBrush Opacity="0.1" Viewport="0,0,250,30" ViewportUnits="Absolute" TileMode="Tile" Stretch="None">
                <VisualBrush.Visual>
                    <Border Width="250" Background="Transparent">
                        <TextBlock Margin="5" FontSize="60px" >
              VisualBrush
              <TextBlock.Foreground>
                <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
                  <LinearGradientBrush.GradientStops>
                    <GradientStop Offset="0.0" Color="Black" />
                    <GradientStop Offset="1.0" Color="Red" />
                  </LinearGradientBrush.GradientStops>
                </LinearGradientBrush>
              </TextBlock.Foreground>
                        </TextBlock>
                    </Border>
                </VisualBrush.Visual>
                <VisualBrush.RelativeTransform>
                    <RotateTransform Angle="0" CenterX="0.5" CenterY="0.5" />
                </VisualBrush.RelativeTransform>
            </VisualBrush>
        </StackPanel.Background>
    </StackPanel>
</Page>

   
    
    
    
    
     


Visual Brush For a Rectangle


   
     

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="250" Width="550">
    <StackPanel Orientation="Horizontal">
        <StackPanel Margin="5" MinWidth="180">
            <TextBlock FontSize="15" Text="Active Controls:"/>
            <StackPanel Name="spInnerLeftPanel" Margin="5">
                <Button Content="Button 2" Margin="5" MinWidth="80"/>
            </StackPanel>
        </StackPanel>
        <Canvas Margin="5">
            <Canvas.Resources>
                <VisualBrush x:Key="VB1" Visual="{Binding ElementName=spInnerLeftPanel}" />
                <VisualBrush x:Key="VB2" Viewbox="0,0,0.5,0.2" 
                             Visual="{Binding ElementName=spInnerLeftPanel}" 
                             Stretch="UniformToFill" TileMode="FlipX" />
            </Canvas.Resources>

            <Rectangle Canvas.Top="5" Canvas.Left="5"
                       Stroke="Black" StrokeThickness="2"                       
                       Height="180" Width="80" 
                       Fill="{StaticResource VB1}" 
                       SnapsToDevicePixels="True" />


        </Canvas>
    </StackPanel>
</Window>

   
    
    
    
    
     


Simulating a reflection with VisualBrush


   
     

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      HorizontalAlignment="Center" VerticalAlignment="Center">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="40"/>
        </Grid.RowDefinitions>

        <Grid x:Name="mainUI" IsSharedSizeScope="True" Background="#EEE">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>

            <GroupBox Header="Position" Grid.Row="0" Margin="4">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" SharedSizeGroup="Left" />
                        <ColumnDefinition Width="80" />
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="X: " />
                    <TextBox Grid.Column="1" />
                </Grid>
            </GroupBox>
        </Grid>
        <Rectangle Grid.Row="1">
            <Rectangle.LayoutTransform>
                <ScaleTransform ScaleY="-1" />
            </Rectangle.LayoutTransform>

            <Rectangle.Fill>
                <VisualBrush Visual="{Binding ElementName=mainUI}" />
            </Rectangle.Fill>

            <Rectangle.OpacityMask>
                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                    <GradientStop Offset="0" Color="Transparent" />
                    <GradientStop Offset="1" Color="White" />
                </LinearGradientBrush>
            </Rectangle.OpacityMask>
        </Rectangle>
    </Grid>
</Page>

   
    
    
    
    
     


VisualBrush and DrawingBrush


   
     
<Window x:Class="WPFBrushes.VisualBrushInXAML"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="VisualBrush" Height="500" Width="300">

  <Window.Resources>
    <DrawingBrush x:Key="MyCustomDrawing">
      <DrawingBrush.Drawing>
        <GeometryDrawing Brush="Red">
          <GeometryDrawing.Geometry>
            <GeometryGroup>
              <EllipseGeometry RadiusX="22" RadiusY="25" Center="25,50" />
              <EllipseGeometry RadiusX="22" RadiusY="55" Center="50,50" />
            </GeometryGroup>
          </GeometryDrawing.Geometry>
          <GeometryDrawing.Pen>
            <Pen Thickness="1.5" Brush="LightBlue" />
          </GeometryDrawing.Pen>
        </GeometryDrawing>
      </DrawingBrush.Drawing>
    </DrawingBrush>
  </Window.Resources>

  <Grid>
    <StackPanel Margin="4,4,4,4">

      <TextBlock Margin="4,4,4,4">Source Visual:</TextBlock>
      <Button Content="DrawingBrush" Height="125" Width="275" Name="MyVisual" 
        Background="{StaticResource MyCustomDrawing}"/>

      <TextBlock Margin="4,4,4,4">VisualBrush:</TextBlock>
      <Button Foreground="Blue" Height="125" Width="275">
        <Button.Background>
          <VisualBrush Visual="{Binding ElementName=MyVisual}"/>
        </Button.Background>
      </Button>
    </StackPanel>
  </Grid>
</Window>

   
    
    
    
    
     


Tiled VisualBrush


   
     

<Window x:Class="WPFBrushes.VisualBrushInXAML"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="VisualBrush" Height="500" Width="300">

  <Window.Resources>
    <DrawingBrush x:Key="MyCustomDrawing">
      <DrawingBrush.Drawing>
        <GeometryDrawing Brush="Red">
          <GeometryDrawing.Geometry>
            <GeometryGroup>
              <EllipseGeometry RadiusX="22" RadiusY="25" Center="25,50" />
              <EllipseGeometry RadiusX="22" RadiusY="55" Center="50,50" />
            </GeometryGroup>
          </GeometryDrawing.Geometry>
          <GeometryDrawing.Pen>
            <Pen Thickness="1.5" Brush="LightBlue" />
          </GeometryDrawing.Pen>
        </GeometryDrawing>
      </DrawingBrush.Drawing>
    </DrawingBrush>
  </Window.Resources>

  <Grid>
    <StackPanel Margin="4,4,4,4">

      <TextBlock Margin="4,4,4,4">Source Visual:</TextBlock>
      <Button Content="DrawingBrush" Height="125" Width="275" Name="MyVisual" 
        Background="{StaticResource MyCustomDrawing}"/>


      <TextBlock Margin="4,4,4,4">Tiled VisualBrush:</TextBlock>
      <Button Foreground="Blue" Height="125" Width="275">
        <Button.Background>
          <VisualBrush Visual="{Binding ElementName=MyVisual}" 
            TileMode="Tile">
            <VisualBrush.Transform>
              <ScaleTransform ScaleX=".25" ScaleY=".25" CenterX=".5" CenterY=".5"/>
            </VisualBrush.Transform>
          </VisualBrush>
        </Button.Background>
      </Button>

    </StackPanel>
  </Grid>
</Window>