RoutedEvents: Key Press Events


   
  
<Window x:Class="RoutedEvents.KeyPressEvents"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="KeyPressEvents" Height="400" Width="400" >
    <StackPanel>
          <DockPanel Margin="5">
            <TextBlock Margin="3" >Type here:</TextBlock>
            <TextBox PreviewKeyDown="KeyEvent" KeyDown="KeyEvent" 
                     PreviewKeyUp="KeyEvent" KeyUp="KeyEvent"
                     PreviewTextInput="TextInput"
                     TextChanged="TextChanged"></TextBox>
          </DockPanel>
      <ListBox Margin="5" Name="lstMessages"></ListBox>
      <CheckBox Margin="5" Name="chkIgnoreRepeat">Ignore Repeated Keys</CheckBox>
      <Button Click="cmdClear_Click" HorizontalAlignment="Right">Clear List</Button>
    </StackPanel>
</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 RoutedEvents
{
    public partial class KeyPressEvents : System.Windows.Window
    {
        public KeyPressEvents()
        {
            InitializeComponent();
        }
                
        private void KeyEvent(object sender, KeyEventArgs e)
        {
            if ((bool)chkIgnoreRepeat.IsChecked &amp;&amp; e.IsRepeat) return;
            
            string message = "Event: " + e.RoutedEvent + " " + " Key: " + e.Key;
            lstMessages.Items.Add(message);            
        }

        private void TextInput(object sender, TextCompositionEventArgs e)
        {
            string message = "Event: " + e.RoutedEvent + " " + " Text: " + e.Text;
            lstMessages.Items.Add(message);
        }

        private void TextChanged(object sender, TextChangedEventArgs e)
        {
            string message = "Event: " + e.RoutedEvent;
            lstMessages.Items.Add(message);
        }

        private void cmdClear_Click(object sender, RoutedEventArgs e)
        {            
            lstMessages.Items.Clear();
        }
    }
}

   
    
     


RoutedEvents: Key Press Events


   
  
<Window x:Class="RoutedEvents.KeyPressEvents"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="KeyPressEvents" Height="400" Width="400" >
    <StackPanel>
          <DockPanel Margin="5">
            <TextBlock Margin="3" >Type here:</TextBlock>
            <TextBox PreviewKeyDown="KeyEvent" KeyDown="KeyEvent" 
                     PreviewKeyUp="KeyEvent" KeyUp="KeyEvent"
                     PreviewTextInput="TextInput"
                     TextChanged="TextChanged"></TextBox>
          </DockPanel>
      <ListBox Margin="5" Name="lstMessages"></ListBox>
      <CheckBox Margin="5" Name="chkIgnoreRepeat">Ignore Repeated Keys</CheckBox>
      <Button Click="cmdClear_Click" HorizontalAlignment="Right">Clear List</Button>
    </StackPanel>
</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 RoutedEvents
{
    public partial class KeyPressEvents : System.Windows.Window
    {
        public KeyPressEvents()
        {
            InitializeComponent();
        }
                
        private void KeyEvent(object sender, KeyEventArgs e)
        {
            if ((bool)chkIgnoreRepeat.IsChecked &amp;&amp; e.IsRepeat) return;
            
            string message = "Event: " + e.RoutedEvent + " " + " Key: " + e.Key;
            lstMessages.Items.Add(message);            
        }

        private void TextInput(object sender, TextCompositionEventArgs e)
        {
            string message = "Event: " + e.RoutedEvent + " " + " Text: " + e.Text;
            lstMessages.Items.Add(message);
        }

        private void TextChanged(object sender, TextChangedEventArgs e)
        {
            string message = "Event: " + e.RoutedEvent;
            lstMessages.Items.Add(message);
        }

        private void cmdClear_Click(object sender, RoutedEventArgs e)
        {            
            lstMessages.Items.Clear();
        }
    }
}

   
    
     


RoutedEvents: Key Modifiers


   
  
<Window x:Class="RoutedEvents.KeyModifiers"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="KeyModifiers" Height="300" Width="300">
  <StackPanel Margin="5">
    <TextBox KeyDown="KeyEvent"></TextBox>
    <TextBlock Name="lblInfo"></TextBlock>
    <Button Click="CheckShift">Check Current Shift State</Button>
  </StackPanel>
</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 RoutedEvents
{
    public partial class KeyModifiers : System.Windows.Window
    {
        public KeyModifiers()
        {
            InitializeComponent();
        }

        private void KeyEvent(object sender, KeyEventArgs e)
        {
            
            lblInfo.Text = "Modifiers: " + e.KeyboardDevice.Modifiers.ToString();

            if ((e.KeyboardDevice.Modifiers &amp; ModifierKeys.Control) == ModifierKeys.Control)
            {
                lblInfo.Text += "
You held the Control key.";                   
            }
        }

        private void CheckShift(object sender, RoutedEventArgs e)
        {
            if (Keyboard.IsKeyDown(Key.LeftShift))
            {
                lblInfo.Text = "The left Shift is held down.";                   
            }
            else
            {
                lblInfo.Text = "The left Shift is not held down.";                   
            }
            
        }
    }
}

   
    
     


RoutedEvents: Focus event


   
  

<Window x:Class="RoutedEvents.Focus"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Focus" Height="250" Width="300">
  <StackPanel Margin="5">
    <Button Margin="5" Padding="30" Name="cmdFocus">Focused</Button>
    <Button Margin="5" Padding="30">Not Focused</Button>
    </StackPanel>
</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 RoutedEvents
{
    public partial class Focus : System.Windows.Window
    {
        public Focus()
        {
            InitializeComponent();
        }
        protected override void OnActivated(EventArgs e)
        {
            base.OnActivated(e);
            cmdFocus.Focus();
        }
    }
}

   
    
     


RoutedEvents: Drag And Drop


   
  
<Window x:Class="RoutedEvents.DragAndDrop"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="DragAndDrop" Height="300" Width="400">
  <StackPanel>
    <TextBox Padding="10" VerticalAlignment="Center" HorizontalAlignment="Center">Drag from this TextBox</TextBox>
    <Label Padding="20" Background="LightGoldenrodYellow" VerticalAlignment="Center"  HorizontalAlignment="Center"
           MouseDown="lblSource_MouseDown">Or this Label</Label>
    <Label Background="LightGoldenrodYellow"
           VerticalAlignment="Center" HorizontalAlignment="Center" Padding="20"
      AllowDrop="True" Drop="lblTarget_Drop">To this Label</Label>
  </StackPanel>
</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 RoutedEvents
{
    public partial class DragAndDrop : System.Windows.Window
    {
        public DragAndDrop()
        {
            InitializeComponent();
        }

        private void lblSource_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Label lbl = (Label)sender;
            DragDrop.DoDragDrop(lbl, lbl.Content, DragDropEffects.Copy);
        }

        private void lblTarget_Drop(object sender, DragEventArgs e)
        {
            ((Label)sender).Content = e.Data.GetData(DataFormats.Text);
        }

        private void lblTarget_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text))
                e.Effects = DragDropEffects.Copy;
            else
                e.Effects = DragDropEffects.None;
        }
    }
}

   
    
     


RoutedEvents: Button Mouse Up Event


   
  
<Window x:Class="RoutedEvents.ButtonMouseUpEvent"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="RoutedEvents" Height="300" Width="300">
    <Grid Margin="5">
      <Button Name="cmd" Click="ButtonClick" MouseUp="ButtonMouseUp">Click me.</Button>
    </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 RoutedEvents
{
    public partial class ButtonMouseUpEvent : System.Windows.Window
    {
        public ButtonMouseUpEvent()
        {
            InitializeComponent();
            cmd.AddHandler(Button.MouseUpEvent, new RoutedEventHandler(Backdoor), true);
        }
        
        private void ButtonClick(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("The Button.Click event occurred.");
        }

        private void ButtonMouseUp(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("mouse up.");
        }

        private void Backdoor(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("The (handled) Button.MouseUp event occurred.");
        }


    }
}

   
    
     


Custom Command by KeyGesture and RoutedUICommand


   
  
<Window x:Class="Commands.CustomCommand"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Commands" Height="300" Width="300" xmlns:local="clr-namespace:Commands">
  <Window.CommandBindings>
    <CommandBinding Command="local:DataCommands.MyCommand" Executed="MyCommand"/>
  </Window.CommandBindings>
  <Grid>
    <Button Margin="5" Command="local:DataCommands.MyCommand">Requery</Button>      
  </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 Commands
{
    public partial class CustomCommand : System.Windows.Window
    {
        public CustomCommand()
        {
            InitializeComponent();
        }
        private void MyCommand(object sender, ExecutedRoutedEventArgs e)
        {
            Console.WriteLine("Control R pressed");
        }
    }
    public class DataCommands
    {
        static RoutedUICommand my;
        static DataCommands()
        {
            InputGestureCollection inputs = new InputGestureCollection();
            inputs.Add(new KeyGesture(Key.R, ModifierKeys.Control, "Ctrl+R"));
            my = new RoutedUICommand("Requery", "Requery", typeof(DataCommands), inputs);
        }
        public static RoutedUICommand MyCommand
        {
            get { return my; }
        }
    }
}