Using a Style resource

image_pdfimage_print


   
      

<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

image_pdfimage_print


   
      

<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

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"
    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

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"
    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

image_pdfimage_print


   
   
<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>

   
    
    
     


PenLineJoin.Round

image_pdfimage_print


   
   
<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.Round" />
        <Rectangle StrokeLineJoin="Round" />
    </Canvas>

</StackPanel>

   
    
    
     


PenLineJoin.Miter

image_pdfimage_print


   
   
<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.Miter" />
        <Rectangle StrokeLineJoin="Miter" />
    </Canvas>

</StackPanel>