WPF ComboBox, force input to UpperCase


I have an editable WPF ComboBox. I need to force the user’s text input to uppercase when they type to filter the ComboBox.

It is  easily done for TextBox because it has  “CharacterCasing” property.However ComboBox does not contain this property .

So some extra work need to do this.

At first create a event name “GotKeyboardFocus” in xaml code

<ComboBox Name="cboTest" IsEditable="True" GotKeyboardFocus="cboTest_GotKeyboardFocus">

here cs code


private void cboTest_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
ComboBox cbo = sender as ComboBox;

if (cbo != null)
{
TextBox txt = cbo.Template.FindName("PART_EditableTextBox", cbo) as TextBox;

if (txt != null)
{
txt.CharacterCasing = CharacterCasing.Upper;
}
}
}

If you have any query please leave a comment.

Advertisements

6 Responses to “WPF ComboBox, force input to UpperCase”

  1. Amin Says:

    Very useful…………..thanks for posting.

  2. atik Says:

    Thank you

  3. Denis Says:

    You’ve find a useful solution. Thank’s a lot.

  4. dan Says:

    I am fairly new to wpf and I understand or at least can read it and see what is happening but how would I come up with the “cbo.Template.FindName(“PART_EditableTextBox”” part of the code without having to ask how to do it?? There is absolutely no way I would have come up with that without asking and yet you know how to do this. How or what do I need to do to learn to come up with this answer on my own??

  5. daniel.w Says:

    Thank you very much. I once put this in page loaded event handler and it failed miserable, throwing exception and such. Turn out that Template.FindName can only work if the ComboBox got focus. I would never find this out by myself.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: