Applies a DrawingBrush and DrawingGroup to draw gridlines as a background of a Grid control.

image_pdfimage_print


   
 

<Window x:Class="DrawingBrushGridline"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Drawing Brush - Gridline" Height="300" Width="300">

  <Grid>
    <Grid.Background>
      <DrawingBrush Viewport="0,0,50,50" ViewportUnits="Absolute"
        TileMode="Tile">
        <DrawingBrush.Drawing>
          <DrawingGroup>
            <DrawingGroup.Children>
              <GeometryDrawing Geometry="M0,0 L50,0">
                <GeometryDrawing.Pen>
                  <Pen Thickness="2"
                    Brush="LightGreen" />
                </GeometryDrawing.Pen>
              </GeometryDrawing>
              <GeometryDrawing Geometry="M0,10 L50,10">
                <GeometryDrawing.Pen>
                  <Pen Thickness="1"
                    Brush="LightGreen" />
                </GeometryDrawing.Pen>
              </GeometryDrawing>
              <GeometryDrawing Geometry="M0,20 L50,20">
                <GeometryDrawing.Pen>
                  <Pen Thickness="1"
                    Brush="LightGreen" />
                </GeometryDrawing.Pen>
              </GeometryDrawing>
              <GeometryDrawing Geometry="M0,30 L50,30">
                <GeometryDrawing.Pen>
                  <Pen Thickness="1"
                    Brush="LightGreen" />
                </GeometryDrawing.Pen>
              </GeometryDrawing>
              <GeometryDrawing Geometry="M0,40 L50,40">
                <GeometryDrawing.Pen>
                  <Pen Thickness="1"
                    Brush="LightGreen" />
                </GeometryDrawing.Pen>
              </GeometryDrawing>
              <GeometryDrawing Geometry="M0,0 L0,50">
                <GeometryDrawing.Pen>
                  <Pen Thickness="2"
                    Brush="LightGreen" />
                </GeometryDrawing.Pen>
              </GeometryDrawing>
              <GeometryDrawing Geometry="M10,0 L10,50">
                <GeometryDrawing.Pen>
                  <Pen Thickness="1"
                    Brush="LightGreen" />
                </GeometryDrawing.Pen>
              </GeometryDrawing>
              <GeometryDrawing Geometry="M20,0 L20,50">
                <GeometryDrawing.Pen>
                  <Pen Thickness="1"
                    Brush="LightGreen" />
                </GeometryDrawing.Pen>
              </GeometryDrawing>
              <GeometryDrawing Geometry="M30,0 L30,50">
                <GeometryDrawing.Pen>
                  <Pen Thickness="1"
                    Brush="LightGreen" />
                </GeometryDrawing.Pen>
              </GeometryDrawing>
              <GeometryDrawing Geometry="M40,0 L40,50">
                <GeometryDrawing.Pen>
                  <Pen Thickness="1"
                    Brush="LightGreen" />
                </GeometryDrawing.Pen>
              </GeometryDrawing>
            </DrawingGroup.Children>
          </DrawingGroup>
        </DrawingBrush.Drawing>
      </DrawingBrush>
    </Grid.Background>
  </Grid>
</Window>

   
     


Create buttons using DrawingImage objects

image_pdfimage_print


   
 

<Window x:Class="Chapter05.DrawingImageExample"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="DrawingImage Example" Height="130" Width="320">
    <Button Name="Select" ToolTip="Select" Margin="5">
      <Image Width="80" Height="80">
        <Image.Source>
          <DrawingImage>
            <DrawingImage.Drawing>
              <GeometryDrawing Brush="LightGray">
                <GeometryDrawing.Geometry>
                  <PathGeometry
                    Figures="M25,75 L 50,0 75,75 60,75 60,100 40,100,40,75Z">
                    <PathGeometry.Transform>
                      <RotateTransform CenterX="50" CenterY="50" Angle="45" />
                    </PathGeometry.Transform>
                  </PathGeometry>
                </GeometryDrawing.Geometry>
                <GeometryDrawing.Pen>
                  <Pen Brush="Gray" Thickness="3" />
                </GeometryDrawing.Pen>
              </GeometryDrawing>
            </DrawingImage.Drawing>
          </DrawingImage>
        </Image.Source>
      </Image>
    </Button>

</Window>

   
     


VisualBrush and TileMode

image_pdfimage_print
   
 

<Window x:Class="Chapter05.VisualBrushExample"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="VisualBrush Example" Height="190" Width="250">
    <StackPanel Margin="5" Grid.Column="0">
      <TextBlock Text="Original button:" Margin="5" />
      <Button Name="Select" ToolTip="Select" Margin="5" Width="40" Height="40">
        <Image Width="30" Height="30">
          <Image.Source>
            <DrawingImage>
              <DrawingImage.Drawing>
                <GeometryDrawing Brush="LightGray">
                  <GeometryDrawing.Geometry>
                    <PathGeometry
                      Figures="M250,75 L 50,10 75,75 60,75 20,100 40,100,40,75Z">
                      <PathGeometry.Transform>
                        RotateTransform
                        CenterX="50"
                        CenterY="50"
                        Angle="45"/>
                      </PathGeometry.Transform>
                    </PathGeometry>
                  </GeometryDrawing.Geometry>
                  <GeometryDrawing.Pen>
                    <Pen Brush="Gray" Thickness="3" />
                  </GeometryDrawing.Pen>
                </GeometryDrawing>
              </DrawingImage.Drawing>
            </DrawingImage>
          </Image.Source>
        </Image>
      </Button>
      <TextBlock Text="Tiled button:" Margin="5" />
      <Button Foreground="Blue" Height="120" Width="120">
        <Button.Background>
          <VisualBrush Visual="{Binding ElementName=Select}"
            Viewport="0,0,40,40" ViewportUnits="Absolute" TileMode="Tile">
          </VisualBrush>
        </Button.Background>
      </Button>
    </StackPanel>

</Window>

   
     


Duplicate VisualBrush

image_pdfimage_print
   
 

<Window x:Class="Chapter05.VisualBrushExample"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="VisualBrush Example" Height="190" Width="250">
    <StackPanel Margin="5" Grid.Column="0">
      <TextBlock Text="Original button:" Margin="5" />
      <Button Name="Select" ToolTip="Select" Margin="5" Width="40" Height="40">
        <Image Width="30" Height="30">
          <Image.Source>
            <DrawingImage>
              <DrawingImage.Drawing>
                <GeometryDrawing Brush="LightGray">
                  <GeometryDrawing.Geometry>
                    <PathGeometry
                      Figures="M250,75 L 50,10 75,75 60,75 20,100 40,100,40,75Z">
                      <PathGeometry.Transform>
                        RotateTransform
                        CenterX="50"
                        CenterY="50"
                        Angle="45"/>
                      </PathGeometry.Transform>
                    </PathGeometry>
                  </GeometryDrawing.Geometry>
                  <GeometryDrawing.Pen>
                    <Pen Brush="Gray" Thickness="3" />
                  </GeometryDrawing.Pen>
                </GeometryDrawing>
              </DrawingImage.Drawing>
            </DrawingImage>
          </Image.Source>
        </Image>
      </Button>


      <TextBlock Text="Copied button:" Margin="5" />
      <Button Height="40" Width="40">
        <Button.Background>
          <VisualBrush Visual="{Binding ElementName=Select}" />
        </Button.Background>
      </Button>
    </StackPanel>

</Window>

   
     


DrawingBrush Opacity from 1 to 0

image_pdfimage_print


   
 
<Window x:Class="Workspace.DockExample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Workspace" Width="640" Height="480">
    <Window.Resources>
    <DrawingGroup x:Key="myDrawing">
      <DrawingGroup.Children>
        <GeometryDrawing>
          <GeometryDrawing.Geometry>
            <GeometryGroup>
              <LineGeometry StartPoint="0,0" EndPoint="1,1" />
              <LineGeometry StartPoint="0,1" EndPoint="1,0" />
            </GeometryGroup>
          </GeometryDrawing.Geometry>
          <GeometryDrawing.Pen>
            <Pen Thickness="0.2" Brush="#3366FF" />
          </GeometryDrawing.Pen>
        </GeometryDrawing>
        <GeometryDrawing Brush="Red"
         Geometry="M 0.4,0.4 L 0.5,0.2 0.6,0.4 0.5,0.5" />
        <GeometryDrawing Brush="Black"
         Geometry="M 0.4,0.4 L 0.2,0.5 0.4,0.6 0.5,0.5" />
      </DrawingGroup.Children>
    </DrawingGroup>
    </Window.Resources>
    <StackPanel>
    <Rectangle Width="100" Height="50" Stroke="Black" StrokeThickness="1">
      <Rectangle.Fill>
        <DrawingBrush Drawing="{StaticResource myDrawing}" Opacity="1.0" />
      </Rectangle.Fill>
    </Rectangle>
    <Rectangle Width="100" Height="50" Stroke="Black" StrokeThickness="1">
      <Rectangle.Fill>
        <DrawingBrush Drawing="{StaticResource myDrawing}" Opacity="0.75" />
      </Rectangle.Fill>
    </Rectangle>
    <Rectangle Width="100" Height="50" Stroke="Black" StrokeThickness="1">
      <Rectangle.Fill>
        <DrawingBrush Drawing="{StaticResource myDrawing}" Opacity="0.5" />
      </Rectangle.Fill>
    </Rectangle>
    <Rectangle Width="100" Height="50" Stroke="Black" StrokeThickness="1">
      <Rectangle.Fill>
        <DrawingBrush Drawing="{StaticResource myDrawing}" Opacity="0.25" />
      </Rectangle.Fill>
    </Rectangle>
    <Rectangle Width="100" Height="50" Stroke="Black" StrokeThickness="1">
      <Rectangle.Fill>
        <DrawingBrush Drawing="{StaticResource myDrawing}" Opacity="0" />
      </Rectangle.Fill>
    </Rectangle>
    </StackPanel>

</Window>

   
     


Ellipse Fill with DrawingBrush

image_pdfimage_print


   
     
<Window x:Class="Workspace.DockExample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Workspace" Width="640" Height="480">
    
        <Ellipse Margin="15,0,0,0" Width="200" Height="200" Stroke="Red" StrokeThickness="1">
          <Ellipse.Fill>
            <DrawingBrush Viewport="0,0,150,50" ViewportUnits="Absolute" TileMode="Tile">
              <DrawingBrush.Drawing>
                <DrawingGroup>
                  <DrawingGroup.Children>                
                    <ImageDrawing ImageSource="c:image.JPG" Rect="0,0,100,50" />
                    <ImageDrawing ImageSource="c:image.JPG" Rect="100,0,50,50" />
                  </DrawingGroup.Children>
                </DrawingGroup>                
              </DrawingBrush.Drawing>
            </DrawingBrush>
          </Ellipse.Fill>
        </Ellipse>
</Window>

   
    
    
    
    
     


Rectangle and Ellipse

image_pdfimage_print


   
     

<Window x:Class="WpfApplication1.ShapesWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ShapesWindow" Height="160" Width="400">
    <Canvas>

        <Rectangle Fill="Blue" Canvas.Left="40" Canvas.Top="40" Width="40" />
        <Ellipse Fill="Green" Stroke="Black" StrokeThickness="2" Canvas.Left = "60" Canvas.Top="120" Width="40" Height="40" />
 
    </Canvas>
</Window>