I have a TextBox that whenever a user types into it I want the text to only be uppercase. For example, if I type in "abc" the actual text in the TextBox and in the backend binding should be "ABC".
In WPF there is the CharacterCasing property, but I can't seem to find that in Windows XAML (or whatever you call a Windows 8 app).
I tried making a converter, but that didn't seem to work:
Converter:
public class UpperCaseConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return value.ToString().ToUpper();
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
return value.ToString().ToUpper();
}
}
XAML:
<TextBox Text="{Binding ElementName=uiMainPage, Path=Company, Mode=TwoWay, Converter={StaticResource ToUpper}}"/>
This is the code I made for it in VB.Net but it should be easy to translate to C#
Make a textchanged
event for your textboxes and call a method giving it your sender
as a textbox
Private Sub AnyTextBox_TextChanged(sender As Object, e As TextChangedEventArgs)
TextBoxToChange = (CType(sender,Textbox))
TextBoxToChange.Text = TextBoxToChange.Text.ToUpper()
TextBoxToChange.SelectionStart = TextBoxToChange.Text.Length
End Sub
The TextChanged
event takes the textbox
and changes the text to uppercase
(The selectionstart
is to stop the selection of the textbox
to go back to 0 which causes to write backwards )
You will have XAML looking like this
<TextBox x:Name="txtTest1"
TextChanged="AnyTextBox_TextChanged"/>
<TextBox x:Name="txtTest2"
TextChanged="AnyTextBox_TextChanged"/>
It is not exactly a converter as you wish but it will do the trick just fine and this will only be 1 method per page