Handles ComboBoxItem Selected events.

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"
    Title="WPF" Height="100" Width="300">
    <StackPanel>
        <ComboBox Name="comboBox" IsEditable="True" Margin="5" SelectionChanged="ComboBox_SelectionChanged">
            <ComboBoxItem Content="ComboBox Item 1" Selected="ComboBoxItem_Selected" />
            <ComboBoxItem Content="ComboBox Item 2" Selected="ComboBoxItem_Selected" />
            <ComboBoxItem Content="ComboBox Item 3" Selected="ComboBoxItem_Selected" IsSelected="True"/>
            <ComboBoxItem Content="ComboBox Item 4" Selected="ComboBoxItem_Selected" />
            <ComboBoxItem Content="ComboBox Item 5" Selected="ComboBoxItem_Selected" />
        </ComboBox>
        <Button Content="Get Selected" Margin="5" Width="100" Click="Button_Click" />
    </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ComboBoxItem item = comboBox.SelectedItem as ComboBoxItem;

            if (item != null)
            {
                MessageBox.Show("Current item: " + item.Content, Title);
            }
            else if (!String.IsNullOrEmpty(comboBox.Text))
            {
                MessageBox.Show("Text entered: " + comboBox.Text, Title);
            }
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            if (!IsInitialized) return;

            ComboBoxItem item = comboBox.SelectedItem as ComboBoxItem;

            if (item != null)
            {
                MessageBox.Show("Selected item: " + item.Content, Title);
            }
        }

        private void ComboBoxItem_Selected(object sender,RoutedEventArgs e)
        {
            if (!IsInitialized) return;

            ComboBoxItem item = e.OriginalSource as ComboBoxItem;

            if (item != null)
            {
                MessageBox.Show(item.Content + " was selected.", Title);
            }
        }
    }
}

   
    
     


ComboBoxItem Content

image_pdfimage_print


   
     

<Window x:Class="SimpleStyles.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="SimpleStyles"
  Background="#F8F8F8">
  <ScrollViewer>
    <WrapPanel>
      <HeaderedItemsControl Header="ComboBox">
        <StackPanel>
          <ComboBox Margin="8">
            <ComboBoxItem Content="First Normal Item" />
            <ComboBoxItem Content="Second Normal Item" />
            <ComboBoxItem Content="Third Normal Item" />
            <ComboBoxItem Content="Fourth Normal Item" />
            <ComboBoxItem Content="Fifth Normal Item" />
          </ComboBox>
          <ComboBox Margin="8" IsEditable="True" Text="Edit Me">
            <ComboBoxItem Content="First Normal Item" />
            <ComboBoxItem Content="Second Normal Item" />
            <ComboBoxItem Content="Third Normal Item" />
            <ComboBoxItem Content="Fourth Normal Item" />
            <ComboBoxItem Content="Fifth Normal Item" />
          </ComboBox>
        </StackPanel>
      </HeaderedItemsControl>
   
    </WrapPanel>
  </ScrollViewer>
</Window>

   
    
    
    
    
     


Content in ComboBox Items

image_pdfimage_print


   
      

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

<ComboBox>
  <Button>Click!</Button>
  <TextBlock>Hello, world</TextBlock>
  <StackPanel Orientation="Horizontal">
    <TextBlock>Ellipse:</TextBlock>
    <Ellipse Fill="Blue" Width="100" />
  </StackPanel>
</ComboBox>
</Page>

   
    
    
    
    
    
     


Set Different Font for Item for ComboBox

image_pdfimage_print


   
       


<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Use ComboBox" Height="300" Width="300">
    <Grid>
        <ComboBox Height="39" HorizontalAlignment="Left" Margin="10,10,0,0" Name="ComboBox1" VerticalAlignment="Top" Width="120" SelectedIndex="0" VerticalContentAlignment="Center">
            <ComboBoxItem>Apple</ComboBoxItem>
            <ComboBoxItem FontFamily="Comic Sans MS" FontSize="18" Foreground="Blue">Banana</ComboBoxItem>
            <ComboBoxItem>Cherry</ComboBoxItem>
        </ComboBox>
    </Grid>
</Window>

   
    
    
    
    
    
    
     


Apply Custom Grouping to a Collection

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

   
    
     


Color Animation

image_pdfimage_print


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

   
    
    
    
    
    
    
     


Highlights the gradient origin and the gradient circle

image_pdfimage_print
   
       

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