Handler for the PreviewKeyDown event on the TextBox

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="Window1" Height="300" Width="300">
  <Grid>
    <StackPanel>
      <TextBox x:Name="uv" PreviewKeyDown="TextBox_PreviewKeyDown" />

    </StackPanel>
  </Grid>
</Window>


//File:Window1.xaml.cs
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            DataContext = this;
        }

        //
        private void TextBox_PreviewKeyDown(object sender, RoutedEventArgs e)
        {
            TextBox textBox = sender as TextBox;

            if (textBox != null)
            {
                textBox.Foreground = Brushes.Firebrick;
            }
        }
    }
}