Adding border to TextBlock with Border

image_pdfimage_print


   
     
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      HorizontalAlignment="Center" VerticalAlignment="Center"
      TextElement.FontFamily="Palatino Linotype"
      FontSize="40">

    <Border BorderBrush="Black" BorderThickness="2" Width="213">
        <TextBlock TextTrimming="None" Text="Too much text." />
    </Border>

</Window>

   
    
    
    
    
     


Border with LinearGradientBrush

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">
  
    <Border Margin="0,10,0,10">
      <Border.Background>
        <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
          <LinearGradientBrush.GradientStops>
            <GradientStop Offset="0.0" Color="#CCCCFF" />
            <GradientStop Offset="1.0" Color="White" />
          </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>
      </Border.Background>
    
    <TextBlock Margin="10" MaxWidth="700">
asdf
    </TextBlock>
    </Border>

</Window>

   
    
    
    
     


Set border thickness

image_pdfimage_print


   
     

<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="300" Width="300">
    <UniformGrid>
        <UniformGrid.Resources>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="FontSize" Value="14" />
                <Setter Property="HorizontalAlignment" Value="Center" />
                <Setter Property="VerticalAlignment" Value="Center" />
            </Style>
        </UniformGrid.Resources>

        <Border BorderBrush="Black" BorderThickness="4px, 4px, 0px, 0px" 
                Margin="10">
            <TextBlock Text="A half border" />
        </Border>

    </UniformGrid>
</Window>

   
    
    
    
    
     


Set border margin

image_pdfimage_print


   
     

<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="300" Width="300">
    <UniformGrid>
        <UniformGrid.Resources>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="FontSize" Value="14" />
                <Setter Property="HorizontalAlignment" Value="Center" />
                <Setter Property="VerticalAlignment" Value="Center" />
            </Style>
        </UniformGrid.Resources>

        <Border CornerRadius="0, 5, 30, 90" Background="LightGray" 
                BorderBrush="LightGray" BorderThickness="2px, 5px, 2px, 5px" 
                Margin="10">
            <TextBlock Text="A wonky border" />
        </Border>

    </UniformGrid>
</Window>

   
    
    
    
    
     


Set border corner radius

image_pdfimage_print


   
     

<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="300" Width="300">
    <UniformGrid>
        <UniformGrid.Resources>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="FontSize" Value="14" />
                <Setter Property="HorizontalAlignment" Value="Center" />
                <Setter Property="VerticalAlignment" Value="Center" />
            </Style>
        </UniformGrid.Resources>

        <Border CornerRadius="15" BorderBrush="Black" 
                BorderThickness="2px, 5px, 2px, 5px" Margin="10">
            <TextBlock Text="A rounded border" />
        </Border>


    </UniformGrid>
</Window>

   
    
    
    
    
     


Display a Border

image_pdfimage_print


   
     
<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="300" Width="300">
    <UniformGrid>
        <UniformGrid.Resources>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="FontSize" Value="14" />
                <Setter Property="HorizontalAlignment" Value="Center" />
                <Setter Property="VerticalAlignment" Value="Center" />
            </Style>
        </UniformGrid.Resources>
        <Border CornerRadius="90" Background="LightBlue" BorderThickness="2px"
                Margin="10">
            <TextBlock Text="A circular border" />
        </Border>
    </UniformGrid>
</Window>

   
    
    
    
    
     


A custom pen to draw the borders

image_pdfimage_print


   
      

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:sys="clr-namespace:System;assembly=mscorlib" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
  <Image>
  <Image.Source>
    <DrawingImage>
      <DrawingImage.Drawing>

        <DrawingGroup>
          <GeometryDrawing>
            <GeometryDrawing.Geometry>
              <GeometryGroup>
                <RectangleGeometry Rect="01,0,20,20" />
                <RectangleGeometry Rect="1160,120,20,20" />
                <EllipseGeometry Center="715,75" RadiusX="50" RadiusY="50" />
                <LineGeometry StartPoint="75,75" EndPoint="180,0" />
              </GeometryGroup>
            </GeometryDrawing.Geometry>
            <GeometryDrawing.Pen>
             <Pen Thickness="10" LineJoin="Round"
                      EndLineCap="Triangle" StartLineCap="Round" DashStyle = "{x:Static DashStyles.DashDotDot}" >
               <Pen.Brush>
                 <LinearGradientBrush>
                   <GradientStop Offset="0.0" Color="Red" />
                   <GradientStop Offset="1.0" Color="Green" />
                 </LinearGradientBrush>
                </Pen.Brush>
              </Pen>
            </GeometryDrawing.Pen>

          </GeometryDrawing>
        </DrawingGroup>
      </DrawingImage.Drawing>
    </DrawingImage>
  </Image.Source>
 </Image>
</Window>