Change Key in WPF KeyDown

Conversion problem for old apps. VB6 TextBox_KeyDown () allows you to change the key (for example, force a key press to uppercase, but there are many other uses). How can this be done in WPF?

The only way I can see is to manage all the keystrokes of the TextBox. In fact, override editing the TextBox. I would rather not go there.

+2


a source to share


2 answers


A very quick and dirty solution. Assuming you want to bind the TextBox.Text value to something, you can write a converter that just calls ToUpper () on the string.

In the example below, the textbox is anchored to itself. This is most likely NOT what you want in production, but it might be inspiring.

<local:UpperConverter x:Key="toUpperConverter" />

      

...



<TextBox Text="{Binding RelativeSource={RelativeSource Mode=Self},
                                Path=Text, Mode=OneWay, Converter={StaticResource toUpperConverter},
                                UpdateSourceTrigger=PropertyChanged}" />

      

...



class UpperConverter:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value.ToString().ToUpper();
        }

      

0


a source


You can set the CharacterCasing to the top one.

<TextBox CharacterCasing="Upper"></TextBox>

      



MSDN Solution

-1


a source







All Articles