Sort Data in 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:ComponentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="WPF" Height="250" Width="200">
    <Window.Resources>
        <local:Countries x:Key="countries"/>
        <CollectionViewSource x:Key="cvs" Source="{Binding Source={StaticResource countries}}">
            <CollectionViewSource.SortDescriptions>
                <ComponentModel:SortDescription PropertyName="Name" />
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>
    </Window.Resources>
    <Grid>
        <ItemsControl ItemsSource="{Binding Source={StaticResource cvs}}" DisplayMemberPath="Name" />
    </Grid>

</Window>
//File:Window.xaml.cs
using System.Collections.ObjectModel;

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

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


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

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

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

        }
    }
}

   
    
     


Filter Data in a Collection, Set the Filter property to a FilterEventHandler


   
  

<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"
    Title="WPF" Height="124" Width="124">
    <Window.Resources>
        <local:Countries x:Key="countries"/>
        <CollectionViewSource x:Key="cvs" Source="{Binding Source={StaticResource countries}}"
            Filter="CollectionViewSource_EuropeFilter" />
    </Window.Resources>
    <Grid>

        <ItemsControl ItemsSource="{Binding Source={StaticResource cvs}}"
            DisplayMemberPath="Name"/>
    </Grid>

</Window>
//File:Window.xaml.cs
using System.Windows;
using System.Windows.Data;
using System.Collections.ObjectModel;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void CollectionViewSource_EuropeFilter(object sender, FilterEventArgs e)
        {
            Country country = e.Item as Country;
            e.Accepted = (country.Continent == Continent.Europe);
        }
    }

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

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

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

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

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

   
    
     


Use InputGestureCollection to get modifier keys


   
  
<Window x:Class="Commands.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Commands">
<Grid>
    <Button VerticalAlignment="Top" 
      HorizontalAlignment="Stretch" 
      Height="27" 
      Click="ExecuteCommandClickEvent" 
      Name="BtnExecuteCommand">Execute Command
  </Button>
  </Grid>
</Window>
//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Commands
{
    public partial class Window1 : Window
    {
       
        public static RoutedCommand myCmd;

        static Window1()
        {
            InputGestureCollection myInputs = new InputGestureCollection();
            myInputs.Add(new KeyGesture(Key.G,ModifierKeys.Control | ModifierKeys.Shift));
            myCmd = new RoutedCommand("Go", typeof(Window1), myInputs);
        }

        private void ExecuteCommandClickEvent(object sender, RoutedEventArgs e)
        {
            myCmd.Execute(sender,null);
        }
    }
}

   
    
     


Use InputBinding to bind Application commends to Key events


   
  


<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="CommandBasics" Height="300" Width="300">
  <Grid>
    <Button Command="ApplicationCommands.Properties" Content="_Properties"/>
  </Grid>

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

namespace WpfApplication1
{
    public partial class Window1 : Window
    {

        public Window1()
        {
            InitializeComponent();

            InputBinding ib = new InputBinding(ApplicationCommands.Properties,new KeyGesture(Key.Enter, ModifierKeys.Alt));
            this.InputBindings.Add(ib);
            CommandBinding cb = new CommandBinding(ApplicationCommands.Properties);
            cb.Executed += new ExecutedRoutedEventHandler(cb_Executed);
            this.CommandBindings.Add(cb);

        }

        void cb_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Properties");
        }
    }
}

   
    
     


TitleMode and Viewport for ImageBrush


   
     


<Window x:Class="BrushTransformExample"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Brush Transforms" Height="475" Width="510">
  <Grid>
    <Rectangle Width="120" Height="60" Margin="5" Grid.Column="1"
      Grid.Row="4">
      <Rectangle.Fill>
        <ImageBrush ImageSource="c:image.jpg" TileMode="Tile" Viewport="0,0,0.5,0.5" />
      </Rectangle.Fill>
    </Rectangle>

  </Grid>
</Window>

   
    
    
    
    
     


Using Viewbox and Viewport


   
     

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      HorizontalAlignment="Stretch" VerticalAlignment="Stretch">

    <Rectangle>
      <Rectangle.Fill>
        <ImageBrush ViewboxUnits="Absolute" Viewbox="300,285,300,243" 
                    Viewport="0.1,0.321,0.7, 0.557" 
                    ImageSource="c:image.jpg" />
      </Rectangle.Fill>
    </Rectangle>

</Page>