Stroke Start Line Cap


   
      
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="FontSize" Value="16" />
            <Setter Property="Margin" Value="24" />
            <Setter Property="VerticalAlignment" Value="Center" />
        </Style>

        <Style TargetType="{x:Type Line}">
            <Setter Property="Grid.Column" Value="1" />
            <Setter Property="Y1" Value="3" />
            <Setter Property="X2" Value="400" />
            <Setter Property="Y2" Value="30" />
            <Setter Property="StrokeThickness" Value="25" />
            <Setter Property="Stroke" Value="Black" />
            <Setter Property="StrokeDashArray" Value="2 2 3 4" />

            <Setter Property="StrokeStartLineCap" Value="{Binding RelativeSource={RelativeSource self}, Path=StrokeDashCap}" />

            <Setter Property="StrokeEndLineCap" Value="{Binding RelativeSource={RelativeSource self}, Path=StrokeDashCap}" />

        </Style>
    </Grid.Resources>

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>

    <!-- PenLineCap.Flat. -->
    <TextBlock Grid.Row="0" Text="PenLineCap.Flat" />
    <Line Grid.Row="0" />

    <!-- PenLineCap.Square. -->
    <TextBlock Grid.Row="1" Text="PenLineCap.Square" />
    <Line Grid.Row="1" StrokeDashCap="Square" />

    <!-- PenLineCap.Round. -->
    <TextBlock Grid.Row="2" Text="PenLineCap.Round" />
    <Line Grid.Row="2" StrokeDashCap="Round" />

    <!-- Triangle.Triangle. -->
    <TextBlock Grid.Row="3" Text="PenLineCap.Triangle" />
    <Line Grid.Row="3" StrokeDashCap="Triangle" />
</Grid>

   
    
    
    
    
    
     


Property Trigger


   
      

<Window x:Class="PropertyTrigger.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="PropertyTrigger" Height="300" Width="300">
  <Window.Resources>

    <Style TargetType="{x:Type Button}">
      <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
          <Setter Property="Background" Value="Orange" />
        </Trigger>
      </Style.Triggers>
    </Style>

  </Window.Resources>

  <Grid>
    <Button 
      VerticalAlignment="Top"
      HorizontalAlignment="Stretch"
      Height="46"
      Name="button1"
      >
      Button
    </Button>
    
  </Grid>
</Window>

   
    
    
    
    
    
     


Using a Style resource


   
      

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="RSS Reader">
      
      
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Orientation="Horizontal">
  <StackPanel.Resources>
    <Style x:Key="buttonStyle">
      <Setter Property="Button.FontSize" Value="22"/>
      <Setter Property="Button.Background" Value="Purple"/>
      <Setter Property="Button.Foreground" Value="White"/>
      <Setter Property="Button.Height" Value="50"/>
      <Setter Property="Button.Width" Value="50"/>
      <Setter Property="Button.RenderTransformOrigin" Value=".5,.5"/>
      <Setter Property="Button.RenderTransform">
        <Setter.Value>
          <RotateTransform Angle="10"/>
        </Setter.Value>
      </Setter>
    </Style>
  </StackPanel.Resources>
  <Button Style="{StaticResource buttonStyle}">1</Button>
</StackPanel>


</Window>

   
    
    
    
    
    
     


Sharing a Style


   
      

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="RSS Reader">
      
    <StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Orientation="Horizontal">
      <StackPanel.Resources>
        <Style x:Key="controlStyle">
          <Setter Property="Control.FontSize" Value="22"/>
          <Setter Property="Control.Background" Value="Purple"/>
          <Setter Property="Control.Foreground" Value="White"/>
          <Setter Property="Control.Height" Value="50"/>
          <Setter Property="Control.Width" Value="50"/>
          <Setter Property="Control.RenderTransformOrigin" Value=".5,.5"/>
          <Setter Property="Control.RenderTransform">
            <Setter.Value>
              <RotateTransform Angle="10"/>
            </Setter.Value>
          </Setter>
        </Style>
      </StackPanel.Resources>
      <ComboBox Style="{StaticResource controlStyle}">
        <ComboBox.Items>2</ComboBox.Items>
      </ComboBox>
      <Expander Style="{StaticResource controlStyle}" Content="3"/>
      <TabControl Style="{StaticResource controlStyle}">
        <TabControl.Items>4</TabControl.Items>
      </TabControl>
      <ToolBar Style="{StaticResource controlStyle}">
        <ToolBar.Items>5</ToolBar.Items>
      </ToolBar>
      <InkCanvas Style="{StaticResource controlStyle}"/>
      <TextBox Style="{StaticResource controlStyle}" Text="7"/>
    </StackPanel>

</Window>

   
    
    
    
    
    
     


Ignore an Implicit Style by setting Style to Null


   
       

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfApplication1="clr-namespace:WpfApplication1;assembly="
    Title="WPF" Height="88" Width="180">

    <Window.Resources>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Background" Value="LightGray"/>
            <Setter Property="FontWeight" Value="Bold"/>
        </Style>
    </Window.Resources>
    <StackPanel Margin="4">
        <Button>Implicit Style</Button>
        <Button Style="{x:Null}">Ignores Style</Button>
    </StackPanel>

</Window>

   
    
    
    
    
    
    
     


Set a Style Programmatically


   
  
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Loaded="Window_Loaded" Title="WPF" Height="230" Width="140">
    <Window.Resources>
        <Style x:Key="labelStyle1">
            <Setter Property="Label.Background" Value="LightYellow" />
            <Setter Property="Label.HorizontalContentAlignment" Value="Center" />
        </Style>
        <Style x:Key="imageStyle1">
            <Setter Property="Image.Source" Value="c:image.png" />
            <Setter Property="Image.Height" Value="140" />
            <Setter Property="Image.Width" Value="96" />
        </Style>
        <Style x:Key="labelStyle2">
            <Setter Property="Label.Background" Value="AliceBlue" />
            <Setter Property="Label.Foreground" Value="DarkBlue" />
        </Style>
        <Style x:Key="imageStyle2">
            <Setter Property="Image.Source" Value="c:image.png" />
            <Setter Property="Image.Height" Value="140" />
            <Setter Property="Image.Width" Value="96" />
        </Style>
    </Window.Resources>
    <StackPanel>
        <Image x:Name="img"/>
        <Label x:Name="lbl" Content="Hello" />
    </StackPanel>
</Window>

//File:Window.xaml.cs

using System;
using System.Windows;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            lbl.Style = (Style)FindResource("labelStyle2");
            img.Style = (Style)FindResource("imageStyle2");
            //lbl.Style = (Style)FindResource("labelStyle1");
            //img.Style = (Style)FindResource("imageStyle1");

        }
    }
}

   
    
     


PenLineJoin.Bevel


   
   
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Orientation="Horizontal">
    <StackPanel.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Canvas.Left" Value="25" />
        </Style>

        <Style TargetType="{x:Type Canvas}">
            <Setter Property="Width" Value="150" />
            <Setter Property="Margin" Value="12" />
        </Style> 

        <Style TargetType="{x:Type Rectangle}">
            <Setter Property="Width" Value="100" />
            <Setter Property="Height" Value="100" />
            <Setter Property="Canvas.Top" Value="50" />
            <Setter Property="Canvas.Left" Value="25" />
            <Setter Property="Stroke" Value="Black" />
            <Setter Property="StrokeThickness" Value="25" />
        </Style>
    </StackPanel.Resources>

    <Canvas>
        <TextBlock Text="PenLineJoin.Bevel" />
        <Rectangle StrokeLineJoin="Bevel" />
    </Canvas>

</StackPanel>