Animates the text block's opacity(fading text)


   
     
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Microsoft.Samples.Graphics.RectangleExample"
    WindowTitle="Example">
  <Canvas>
      <TextBlock
        Name="MyFadingText"
        Margin="20" 
        Width="640" Height="100" FontSize="48" FontWeight="Bold" Foreground="Maroon">
        This is fading text

        <TextBlock.Triggers>
          <EventTrigger RoutedEvent="TextBlock.Loaded">
            <BeginStoryboard>
              <Storyboard>
                <DoubleAnimation
                  Storyboard.TargetName="MyFadingText" 
                  Storyboard.TargetProperty="(TextBlock.Opacity)"
                  From="1.0" To="0.0" Duration="0:0:5" 
                  AutoReverse="True" RepeatBehavior="Forever" />
              </Storyboard>
            </BeginStoryboard>
          </EventTrigger>
        </TextBlock.Triggers>
      </TextBlock>

  </Canvas>
</Page>

   
    
    
    
    
     


Animates the text block's width


   
     
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Microsoft.Samples.Graphics.RectangleExample"
    WindowTitle="Example">
  <Canvas>
      <TextBlock
        Name="MyWipedText"
        Margin="20" 
        Width="480" Height="100" FontSize="48" FontWeight="Bold" Foreground="Maroon">
        This is wiped text

        <TextBlock.Triggers>
          <EventTrigger RoutedEvent="TextBlock.Loaded">
            <BeginStoryboard>
              <Storyboard>
                <DoubleAnimation
                  Storyboard.TargetName="MyWipedText" 
                  Storyboard.TargetProperty="(TextBlock.Width)"
                  To="0.0" Duration="0:0:10" 
                  AutoReverse="True" RepeatBehavior="Forever" />
              </Storyboard>
            </BeginStoryboard>
          </EventTrigger>
        </TextBlock.Triggers>
      </TextBlock>

  </Canvas>
</Page>

   
    
    
    
    
     


TextEffect to animate


   
     
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  WindowTitle="TextEffect Sample" Background="SteelBlue">

    <StackPanel Margin="40">
        <Border Name="TextBorder" HorizontalAlignment="Center"
      VerticalAlignment="Center">
            <TextBlock 
        FontSize="60"
        Margin="50"
        Foreground="White">
        Windows Vista

        <TextBlock.TextEffects>

          <TextEffect PositionCount="1" x:Name="MyTextEffect">
            <TextEffect.Transform>
              <RotateTransform x:Name="TextEffectRotateTransform" 
                Angle="0" CenterX="10" CenterY="10" />
            </TextEffect.Transform>
          </TextEffect>
        </TextBlock.TextEffects>
    </TextBlock>
    </Border>

    </StackPanel>
</Page>

   
    
    
    
    
     


Animates the horizontal center of the RotateTransform applied to the TextEffect

   
     

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  WindowTitle="TextEffect Sample" Background="SteelBlue">

  <StackPanel Margin="40">
    <Border Name="TextBorder" HorizontalAlignment="Center"
      VerticalAlignment="Center">
      <TextBlock 
        FontSize="60"
        Margin="50"
        Foreground="White">
        Windows Vista

        <TextBlock.Triggers>
          <EventTrigger RoutedEvent="TextBlock.Loaded">
            <EventTrigger.Actions>
            <BeginStoryboard>
              <Storyboard>
                <ParallelTimeline RepeatBehavior="Forever">

                <DoubleAnimation
                  From="30"
                  To="370"
                  Duration="00:00:13"
                  RepeatBehavior="Forever"
                  AutoReverse="True"
                  Storyboard.TargetName="TextEffectRotateTransform"
                  Storyboard.TargetProperty="CenterX" />
                </ParallelTimeline>
              </Storyboard>
            </BeginStoryboard>
            </EventTrigger.Actions>
          </EventTrigger>
        </TextBlock.Triggers>
      </TextBlock>

    </Border>
  </StackPanel>
</Page>

   
    
    
    
    
     


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

   
    
     


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

   
    
     


Reference SystemParameters


   
  
<Window x:Class="Windows.CenterScreen"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="CenterScreen" Height="300" Width="300"
    >
  <Button Margin="15" Click="cmdCenter_Click">Center Me</Button>
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;

namespace Windows
{
    public partial class CenterScreen : System.Windows.Window
    {
        public CenterScreen()
        {
            InitializeComponent();
        }

        private void cmdCenter_Click(object sender, RoutedEventArgs e)
        {            
            double height = SystemParameters.WorkArea.Height;
            double width = SystemParameters.WorkArea.Width;            
            this.Top = (height - this.Height) / 2;            
            this.Left = (width - this.Width) / 2;            
         
        }
    }
}