Set AccessText for Label


   
   

<StackPanel
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="LabelSimple.Pane1">

    <DockPanel Margin="30,10,3,3" Grid.Column="0" Grid.Row="4">
        <TextBox Name="textBox1" Width="50" Height="20"/>
        <Label Width="200" HorizontalAlignment="Left"
             Target="{Binding ElementName=textBox1}">
            <AccessText TextWrapping="WrapWithOverflow">
                _Another long piece of text that requires text wrapping
          goes here.
            </AccessText>
        </Label>
    </DockPanel>


</StackPanel>

   
    
    
     


Text Wrapping Label


   
   
<StackPanel
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="LabelSimple.Pane1">

    <DockPanel Margin="30,10,3,3" Grid.Column="0" Grid.Row="3">
        <Label Width="200" HorizontalAlignment="Left">
            <TextBlock TextWrapping="WrapWithOverflow">
          A long piece of text that requires text wrapping
          goes here.
            </TextBlock>
        </Label>
    </DockPanel>

</StackPanel>

   
    
    
     


Create Label controls that have access keys and support text wrapping.


   
   

<StackPanel
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="LabelSimple.Pane1">

            <DockPanel Margin="30,10,3,3" Grid.Column="0" Grid.Row="2">
            <!--<Snippet1>-->
            <TextBox Name="tb" Width="50"/>
            <Label Target="{Binding ElementName=tb}">_File (Press Alt to see the underline)</Label>
            <!--</Snippet1>-->
        </DockPanel>
    
</StackPanel>

   
    
    
     


On Key Down Handler


   
  


<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="KeyDown Sample" Height="400" Width="500">
  <StackPanel>
    <TextBlock Width="300" Height="20">
      Type some text into the TextBox and press the Enter key.
    </TextBlock>
    <TextBox Width="300" Height="30" Name="textBox1"
             KeyDown="OnKeyDownHandler"/>
    <TextBlock Width="300" Height="100" Name="textBlock1"/>
  </StackPanel>
</Window>

//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;


namespace WpfApplication1
{
    public partial class Window1 : Window
    {

        public Window1()
        {
            InitializeComponent();
        }
        private void OnKeyDownHandler(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                textBlock1.Text = "You Entered: " + textBox1.Text;
            }
        }
    }
}

   
    
     


If input is not a number do not handle the key event


   
  


<Window x:Class="RoutedEvents.OnlyNumbers"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Only Numbers" Height="300" Width="300">
    <StackPanel Margin="5" PreviewTextInput="pnl_PreviewTextInput" PreviewKeyDown="pnl_PreviewKeyDown">
        <TextBox Margin="3" AcceptsTab="False"></TextBox>
        <TextBox Margin="3"></TextBox>
        <TextBox Margin="3"></TextBox>
    </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 OnlyNumbers : System.Windows.Window
    {
        public OnlyNumbers()
        {
            InitializeComponent();
        }
        private void pnl_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            short val;
            if (!Int16.TryParse(e.Text, out val))
            {
                e.Handled = true;
            }            
        }

        private void pnl_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
        }
    }
}

   
    
     


StackPanel PreviewTextInput and PreviewKeyDown


   
  


<Window x:Class="RoutedEvents.OnlyNumbers"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Only Numbers" Height="300" Width="300">
    <StackPanel Margin="5" PreviewTextInput="pnl_PreviewTextInput" PreviewKeyDown="pnl_PreviewKeyDown">
        <TextBox Margin="3" AcceptsTab="False"></TextBox>
        <TextBox Margin="3"></TextBox>
        <TextBox Margin="3"></TextBox>
    </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 OnlyNumbers : System.Windows.Window
    {
        public OnlyNumbers()
        {
            InitializeComponent();
        }
        private void pnl_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            short val;
            if (!Int16.TryParse(e.Text, out val))
            {
                e.Handled = true;
            }            
        }

        private void pnl_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
        }
    }
}

   
    
     


Use KeyBinding to bind Key event to TextBox.InputBindings


   
  
<Window x:Class="Commands.NoCommandTextBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="NoCommandTextBox" Height="300" Width="300">
    <Grid>
      <TextBox Name="txt"/>
    </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 NoCommandTextBox : System.Windows.Window
    {
        public NoCommandTextBox()
        {
            InitializeComponent();           
        
            txt.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.C, ModifierKeys.Control));
        }
     
        private void SuppressCommand(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = false;
            e.Handled = true;
        }
    }
}