Highlights the gradient origin and the gradient circle

   
       

<Window x:Class="WpfApplication1.ShapesWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ShapesWindow" Height="160" Width="400">

    <StackPanel>

    <Canvas ClipToBounds="True" Grid.Row="3" Grid.Column="2" Width="150" Height="150">
      <Rectangle Width="150" Height="150">
        <Rectangle.Fill>
          <RadialGradientBrush GradientOrigin="0.5,0.5" Center="0.1,0.1" RadiusX="0.75" RadiusY="0.75">
            <GradientStop Color="White" Offset="0" />
            <GradientStop Color="#545454" Offset="1" />
          </RadialGradientBrush>
        </Rectangle.Fill>
      </Rectangle>ss
      <Path Fill="Red">
        <Path.Data>
          <EllipseGeometry Center="75,75" RadiusX="2" RadiusY="2" />
        </Path.Data>
      </Path>
      <Path Stroke="Red" StrokeThickness="2">
        <Path.Data>
          <EllipseGeometry Center="15,15" RadiusX="111.5" RadiusY="111.5" />
        </Path.Data>
      </Path>
    </Canvas>

    </StackPanel>
</Window>

   
    
    
    
    
    
    
     


Color Animation


   
       
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Background="Red">
    <Canvas.Triggers>
        <EventTrigger RoutedEvent="Canvas.Loaded">
            <BeginStoryboard>
                <Storyboard TargetProperty="Background.Color">
                    <ColorAnimationUsingKeyFrames RepeatBehavior="Forever">
                        <LinearColorKeyFrame KeyTime="0:0:0" Value="Red" />
                        <LinearColorKeyFrame KeyTime="0:0:1" Value="Orange" />

                        <LinearColorKeyFrame KeyTime="0:0:7" Value="Red" />
                    </ColorAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Canvas.Triggers>
</Canvas>

   
    
    
    
    
    
    
     


Apply Custom Grouping to a Collection


   
  

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

   
    
     


Create CollectionViewSource























//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;
using System.Collections.ObjectModel;

namespace WpfApplication1 {

public class Employee : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
protected void Notify(string propName) {
if( this.PropertyChanged != null ) {
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}

string name;
public string Name {
get { return this.name; }
set {
if( this.name == value ) { return; }
this.name = value;
Notify(“Name”);
}
}

int age;
public int Age {
get { return this.age; }
set {
if( this.age == value ) { return; }
this.age = value;
Notify(“Age”);
}
}

public Employee() { }
public Employee(string name, int age) {
this.name = name;
this.age = age;
}
}

class People : ObservableCollection { }

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

}

public class AgeToRangeConverter : IValueConverter {

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
return (int)value < 25 ? "Under 25" : "Over 25"; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } [/csharp]

Use CollectionViewSource to sort and group data in XAML.


   
  


<Window x:Class="WpfApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
  xmlns:src="clr-namespace:WpfApplication1"
  xmlns:dat="clr-namespace:System.Windows.Data;assembly=PresentationFramework" 
  Title="CollectionViewSourceSample">

  <Window.Resources>

    <src:Places x:Key="places"/>

    <CollectionViewSource Source="{StaticResource places}" x:Key="cvs">
      <CollectionViewSource.SortDescriptions>
        <scm:SortDescription PropertyName="CityName"/>
      </CollectionViewSource.SortDescriptions>
      <CollectionViewSource.GroupDescriptions>
        <dat:PropertyGroupDescription PropertyName="State"/>
      </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>

  </Window.Resources>
  <DockPanel>
    <ListBox ItemsSource="{Binding Source={StaticResource cvs}}" DisplayMemberPath="CityName" Name="lb">
      <ListBox.GroupStyle>
        <x:Static Member="GroupStyle.Default"/>
      </ListBox.GroupStyle>
    </ListBox>
  </DockPanel>
</Window>

//File:Window.xaml.cs

using System.Windows;
using System.Collections.ObjectModel;

namespace WpfApplication1
{

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


    }

    public class Place
    {
        private string name;

        private string state;

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

        public string State
        {
            get { return state; }
            set { state = value; }
        }

        public Place()
        {
            this.name = "";
            this.state = "";
        }

        public Place(string name, string state)
        {
            this.name = name;
            this.state = state;
        }
    }

    public class Places : ObservableCollection<Place>
    {
        public Places()
        {
            Add(new Place("A", "WA"));
            Add(new Place("B", "WA"));
            Add(new Place("C", "WA"));
        }
    }
}

   
    
     


Dynamic Clipping


   
  
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:d="http://schemas.microsoft.com/expression/interactivedesigner/2006"
  mc:Ignorable="d"
  x:Class="PaintDrawExamples.DynamicClipping" 
  Width="640" Height="480">

    <StackPanel.Resources>
        <Storyboard x:Key="OnLoaded"/>
    </StackPanel.Resources>
    <StackPanel.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard x:Name="OnLoaded_BeginStoryboard" Storyboard="{DynamicResource OnLoaded}"/>
        </EventTrigger>
    </StackPanel.Triggers>
    <Canvas Height="100" x:Name="Canvas" Width="436">
        <Canvas.Clip>
            <PathGeometry>
                <PathFigure StartPoint="1,5" IsClosed="True" IsFilled="True">
                    <BezierSegment IsSmoothJoin="True" Point1="2,2" Point2="26,1" Point3="24,127" IsStroked="True"/>
                    <BezierSegment IsSmoothJoin="True" Point1="1,1" Point2="14,9" Point3="19,5" IsStroked="True"/>
                    <BezierSegment IsSmoothJoin="True" Point1="14,11" Point2="18,-22.5" Point3="24,-2" IsStroked="True"/>
                    <BezierSegment IsSmoothJoin="True" Point1="26,-200" Point2="29,1" Point3="300,5" IsStroked="True"/>
                </PathFigure>
            </PathGeometry>
        </Canvas.Clip>
        <Rectangle d:LayoutOverrides="Height" Stroke="{x:Null}" Fill="Red" Width="436" Height="100" x:Name="Rectangle" Canvas.Left="0" Canvas.Top="0"/>
        <Label Background="Black" x:Name="Label" Content="This is my clipped space." Canvas.Left="46" Canvas.Top="26" d:IsHidden="True"/>
    </Canvas>
</StackPanel>
//File:Window.xaml.cs

using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;

namespace PaintDrawExamples
{
    public partial class DynamicClipping
    {
        public DynamicClipping()
        {
            this.InitializeComponent();
            this.Canvas.VerticalAlignment = VerticalAlignment.Center;
            this.Canvas.HorizontalAlignment = HorizontalAlignment.Center;

            CompositionTarget.Rendering += CompositionTarget_Rendering;
        }

        private void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            Point mousePos = Mouse.GetPosition(this.Canvas);
            Geometry clippingRegion = this.Canvas.Clip;

            TranslateTransform newPos = new TranslateTransform();
            newPos.X = mousePos.X - (this.Canvas.Width / 2);
            newPos.Y = mousePos.Y - (this.Canvas.Height / 2);

            clippingRegion.Transform = newPos;
        }
    }
}

   
    
     


Clipping With Viewbox


   
      

<Window x:Class="Drawing.ClippingWithViewbox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ClippingWithViewbox" Height="335.2" Width="401.6"
    >
  <Window.Resources>
    <GeometryGroup x:Key="clipGeometry" FillRule="Nonzero">
      <EllipseGeometry RadiusX="75" RadiusY="50" Center="100,150"></EllipseGeometry>
      <EllipseGeometry RadiusX="100" RadiusY="25" Center="200,150"></EllipseGeometry>
      <EllipseGeometry RadiusX="75" RadiusY="130" Center="140,140"></EllipseGeometry>
    </GeometryGroup>
  </Window.Resources>
  <Grid>
    <Viewbox >
      <Button Width="350" Height="350" Clip="{StaticResource clipGeometry}">A button</Button>
    </Viewbox>
  </Grid>
</Window>