Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Rectangle filled with horizontal or vertical lines

Tags:

wpf

drawing

I am trying to create one rectangle filled with Horizontal or Vertical lines. The width of the rectangle is dynamic so I can not use an image brush. Please let me know if anybody knows any solution.

like image 557
Pravin Avatar asked May 20 '10 15:05

Pravin


2 Answers

I figured out a straight-forward way of doing this; finally, I used following visual brush resources to fill rectangle with horizontal, vertical or dotted vertical lines respectively

   <!--for horizontal lines-->    
   <VisualBrush 
              x:Key="HorizontalLines" 
              TileMode="Tile" Viewport="0,0,4,4" 
              ViewportUnits="Absolute" Viewbox="0,0,10,10"    
              ViewboxUnits="Absolute">
            <VisualBrush.Visual>
                <Canvas>
                    <Path Stroke="Black" Data="M 0 10 l 10 0" />
                </Canvas>
            </VisualBrush.Visual>
        </VisualBrush>

        <!--For vertical lines-->
        <VisualBrush 
              x:Key="VerticalLines" 
              TileMode="Tile" Viewport="0,0,4,4" 
              ViewportUnits="Absolute" Viewbox="0,0,10,10"    
              ViewboxUnits="Absolute">
            <VisualBrush.Visual>
                <Canvas >            
                    <Path Stroke="Black" Data="M 0 0 l 0 10" />                        
                </Canvas>
            </VisualBrush.Visual>
        </VisualBrush>


         <!--For dotted vertical lines-->
        <VisualBrush 
                x:Key="DottedVerticalLinesWithFill" 
                TileMode="Tile" Viewport="0,0,10,10" 
                ViewportUnits="Absolute" Viewbox="0,0,10,10"    
                ViewboxUnits="Absolute">
            <VisualBrush.Visual>
                <Canvas>
                    <Path Stroke="Purple" Data="M 0 5l 0 -10" />
                </Canvas>
            </VisualBrush.Visual>
        </VisualBrush>
like image 139
Pravin Avatar answered Sep 24 '22 22:09

Pravin


You can easily do this with a LinearGradientBrush:

  <Rectangle Width="100" Height="100">
     <Rectangle.Fill>
        <LinearGradientBrush SpreadMethod="Reflect" StartPoint="0 0" EndPoint="0 0.05">
           <GradientStop Offset="0.5" Color="Black"/>
           <GradientStop Offset="0.5" Color="White"/>
        </LinearGradientBrush>
     </Rectangle.Fill>
  </Rectangle>

You control line thickness and orientation with the EndPoint property.

like image 26
bitbonk Avatar answered Sep 26 '22 22:09

bitbonk