Clipping


   
      

<Window x:Class="Drawing.Clipping"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Clipping" Height="352" Width="707.2"
    >
  <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>

    <Button Clip="{StaticResource clipGeometry}">A button</Button>
    <Image Grid.Column="1" Clip="{StaticResource clipGeometry}" 
           Stretch="None"  Source="c:image.jpg"></Image>
  </Grid>
</Window>

   
    
    
    
    
    
     


Clipped Button


   
      
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Button HorizontalAlignment="Center" VerticalAlignment="Center"
            FontSize="24" Width="200" Height="100"
            Clip="M  0 50 A 100 50 0 0 0 200 50
                          A 100 50 0 0 0   0 50
                  M 90 50 A  10 10 0 0 0 110 50
                          A  10 10 0 0 0  90 50" >
        Clipped Button
    </Button>

</Grid>

   
    
    
    
    
    
     


CheckBox List


   
  

<Window x:Class="ClassicControls.CheckBoxList"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="CheckBoxList" Height="300" Width="300">
  <Grid Margin="10">
    <Grid.RowDefinitions>
      <RowDefinition Height="*"></RowDefinition>
      <RowDefinition Height="Auto"></RowDefinition>
    </Grid.RowDefinitions>
    
    <ListBox Name="lst" SelectionChanged="lst_SelectionChanged" CheckBox.Click="lst_SelectionChanged" >
      <CheckBox Margin="3">Option 1</CheckBox>
      <CheckBox Margin="3">Option 2</CheckBox>
      <CheckBox Margin="3">Option 3</CheckBox>
    </ListBox>
    <StackPanel Grid.Row="1" Margin="0,10,0,0">
      <Button Margin="0,10,0,0" Click="cmd_CheckAllItems">Examine All Items</Button>
    </StackPanel>
  </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 ClassicControls
{
    public partial class CheckBoxList : System.Windows.Window
    {
        public CheckBoxList()
        {
            InitializeComponent();
        }
        private void lst_SelectionChanged(object sender, RoutedEventArgs e)
        {
            if (e.OriginalSource is CheckBox)
            {
                lst.SelectedItem = e.OriginalSource;
            }
            
            if (lst.SelectedItem == null) return;
            Console.WriteLine(lst.SelectedIndex);
            Console.WriteLine(((CheckBox)lst.SelectedItem).IsChecked);
        }

        private void cmd_CheckAllItems(object sender, RoutedEventArgs e)
        {
            foreach (CheckBox item in lst.Items)
            {
                if (item.IsChecked == true)
                {
                    Console.WriteLine((item.Content + " is checked."));

                }
            }
        }
    }
}

   
    
     


Check the CheckBox based on key pressed states


   
  

<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="170" Width="200">
    <StackPanel HorizontalAlignment="Center">
        <UniformGrid Columns="2">
            <UniformGrid.Resources>
                <Style TargetType="{x:Type CheckBox}">
                    <Setter Property="IsHitTestVisible" Value="False" />
                    <Setter Property="Margin" Value="5" />
                </Style>
            </UniformGrid.Resources>
            <CheckBox Content="LeftControl" Name="chkLControl"/>
            <CheckBox Content="RightControl" Name="chkRControl"/>

        </UniformGrid>
        <Button Content="Check Keyboard" Margin="10" Click="Button_Click"/>
    </StackPanel>
</Window>

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

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            CheckKeyboardState();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            CheckKeyboardState();
        }
        private void CheckKeyboardState()
        {
            chkLControl.IsChecked = Keyboard.IsKeyDown(Key.LeftCtrl);
            chkRControl.IsChecked = Keyboard.IsKeyDown(Key.RightCtrl);
            
        }
    }
}

   
    
     


Use Linq to get checked CheckBox


   
  

<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="250" Width="300">
    <StackPanel Name="panel">
        <CheckBox Checked="CheckBox_Checked" Content="First CheckBox" 
                  IsChecked="True" Margin="2" Name="checkbox1" 
                  />
        <Button Content="Get Selected" Margin="5" MaxWidth="100" 
                Click="Button_Click" />
        <TextBlock FontWeight="Bold" Text="Selected CheckBoxes:" />
        <ListBox Margin="5" MinHeight="2cm" Name="listbox" />
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System.Linq;
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)
        {
            listbox.Items.Clear();
            foreach (CheckBox checkbox in panel.Children.OfType<CheckBox>().Where( cb => cb.IsChecked == true))
            {
                listbox.Items.Add(checkbox.Name);
            }
        }
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            if (!IsInitialized) return;
            CheckBox checkbox = e.OriginalSource as CheckBox;
            if (checkbox != null)
            {
                MessageBox.Show(checkbox.Name + " is checked.", Title);
            }
        }


    }
}

   
    
     


Handle CheckBox checked 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="WPF" Height="250" Width="300">
    <StackPanel Name="panel">
        <CheckBox Checked="CheckBox_Checked" Content="First CheckBox" 
                  IsChecked="True" Margin="2" Name="checkbox1"/>
        <Button Content="Get Selected" Margin="5" MaxWidth="100" 
                Click="Button_Click" />
        <TextBlock FontWeight="Bold" Text="Selected CheckBoxes:" />
        <ListBox Margin="5" MinHeight="2cm" Name="listbox" />
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System.Linq;
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)
        {
            listbox.Items.Clear();
            foreach (CheckBox checkbox in panel.Children.OfType<CheckBox>().Where( cb => cb.IsChecked == true))
            {
                listbox.Items.Add(checkbox.Name);
            }
        }
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            if (!IsInitialized) return;
            CheckBox checkbox = e.OriginalSource as CheckBox;
            if (checkbox != null)
            {
                MessageBox.Show(checkbox.Name + " is checked.", Title);
            }
        }

    }
}

   
    
     


Handle CheckBox Unchecked 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="WPF" Height="250" Width="300">
    <StackPanel Name="panel">
        <CheckBox Content="First CheckBox" 
                  IsChecked="True" Margin="2" Name="checkbox1" 
                  Unchecked="CheckBox_Unchecked"/>
        <CheckBox Content="Second CheckBox" 
                  IsChecked="False" Margin="2" Name="checkbox2" 
                  Unchecked="CheckBox_Unchecked"/>
        <Button Content="Get Selected" Margin="5" MaxWidth="100" 
                Click="Button_Click" />
        <TextBlock FontWeight="Bold" Text="Selected CheckBoxes:" />
        <ListBox Margin="5" MinHeight="2cm" Name="listbox" />
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System.Linq;
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)
        {
            listbox.Items.Clear();
            foreach (CheckBox checkbox in panel.Children.OfType<CheckBox>().Where( cb => cb.IsChecked == true))
            {
                listbox.Items.Add(checkbox.Name);
            }
        }

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

            CheckBox checkbox = e.OriginalSource as CheckBox;

            if (checkbox != null)
            {
                MessageBox.Show(checkbox.Name + " is unchecked.", Title);
            }
        }
    }
}