views:

304

answers:

1

Legacy app conversion issue. VB6 TextBox_KeyDown() allows key to be changed (e.g. force keystroke to upper case but there are many other uses). How can this be done in WPF?

The only way I can see is too handle all TextBox keystrokes. In effect, reimplement TextBox editing. I'd rather not go there.

A: 

Very quick and dirty solution. Assuming you want to bind the TextBox.Text value to something, you could write a converter which simply calls ToUpper() on the string.

In the sample below the textbox is bound to itself. This is most likely NOT what you want in production, but it possible can inspire.

<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();
        }
Tendlon
Very imaginative approach. I don't believe it will work for my situation. I need to logically change the e.Key, not visually change the Text property.
BSalita
I see, but unless I am missing something, you could use a converter like showed above, only that you don't bind to the TextBox itself, you bind to the value you to be UpperStrokes.You can of course do editing of the actual key strokes, but that seems to me (again, if I am not missing something) to be overworking. Could you post some concrete scenario/samples and possibly some code?
Tendlon