The default GroupStyle indents the items in a group


   
  

<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"
    Title="WPF" Height="300" Width="160">

    <Window.Resources>
        <WpfApplication1:Countries x:Key="countries"/>
        <WpfApplication1:GroupByContinentConverter x:Key="GroupByContinentConverter"/>
        <CollectionViewSource 
            x:Key="cvs" 
            Source="{Binding Source={StaticResource countries}}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription Converter="{StaticResource GroupByContinentConverter}" />
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    </Window.Resources>
    <Grid>
        <ItemsControl ItemsSource="{Binding Source={StaticResource cvs}}" DisplayMemberPath="Name" >
            <ItemsControl.GroupStyle>
                <x:Static Member="GroupStyle.Default"/>
            </ItemsControl.GroupStyle>
        </ItemsControl>

    </Grid>
</Window>
//File:Window.xaml.cs

using System;
using System.Globalization;
using System.Windows.Data;
using System.Collections.ObjectModel;

namespace WpfApplication1
{
    public class GroupByContinentConverter : IValueConverter
    {
        public object Convert(object value,Type targetType,object parameter,CultureInfo culture)
        {
            Country country = (Country)value;
            // Decide which group the country belongs in
            switch (country.Continent)
            {
                case Continent.NorthAmerica:
                    return "Americas";
                default:
                    return "Others";
            }
        }

        public object ConvertBack(object value,Type targetType,object parameter,CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class Country
    {
        private string name;
        private Continent continent;

        public Country(string name, Continent continent)
        {
            this.name = name;
            this.continent = continent;
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public Continent Continent
        {
            get
            {
                return continent;
            }
            set
            {
                continent = value;
            }
        }
    }

    public enum Continent
    {
        Europe,
        NorthAmerica,
    }

    public class Countries : Collection<Country>
    {
        public Countries()
        {
            this.Add(new Country("Great Britan", Continent.Europe));
            this.Add(new Country("USA", Continent.NorthAmerica));
            this.Add(new Country("Canada", Continent.NorthAmerica));
        }
    }
}

   
    
     


extends StyleSelector to create your own style selector


   
  
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1;assembly="
    Title="WPF" Height="248" Width="128">
    <Window.Resources>
        <local:Countries x:Key="countries"/>
        <Style x:Key="AlternateStyle">
            <Setter Property="ListBoxItem.Background" Value="LightGray"/>
        </Style>
    </Window.Resources>
    <Grid>
        <ListBox DisplayMemberPath="Name" ItemsSource="{Binding Source={StaticResource countries}}" >
            <ListBox.ItemContainerStyleSelector>
                <local:AlternatingRowStyleSelector AlternateStyle="{StaticResource AlternateStyle}" />
            </ListBox.ItemContainerStyleSelector>
        </ListBox>
    </Grid>
</Window>
//File:Window.xaml.cs
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
namespace WpfApplication1
{
    public class AlternatingRowStyleSelector : StyleSelector
    {
        public Style DefaultStyle
        {
            get;
            set;
        }

        public Style AlternateStyle
        {
            get;
            set;
        }

        private bool isAlternate = false;

        public override Style SelectStyle(object item, DependencyObject container)
        {
            Style style = isAlternate ? AlternateStyle : DefaultStyle;

            isAlternate = !isAlternate;

            return style;
        }
    }

    public class Country
    {
        private string name;
        private Continent continent;

        public Country(string name, Continent continent)
        {
            this.name = name;
            this.continent = continent;
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public Continent Continent
        {
            get
            {
                return continent;
            }
            set
            {
                continent = value;
            }
        }
    }
    public enum Continent
    {
        Europe,
        NorthAmerica,
    }
    public class Countries : Collection<Country>
    {
        public Countries()
        {
            this.Add(new Country("Great Britan", Continent.Europe));
            this.Add(new Country("USA", Continent.NorthAmerica));
            this.Add(new Country("Canada", Continent.NorthAmerica));
        }
    }
}

   
    
     


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

        }
    }
}

   
    
     


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>

   
    
    
    
    
    
    
     


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>

   
    
    
    
    
    
     


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>

   
    
    
    
    
    
     


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>