Is there a way to automatically capitalize all input throughout a WPF app?
Asked
Active
Viewed 2.2k times
5 Answers
83
You can case all input into TextBox
controls with the following property:
CharacterCasing="Upper"
To apply to all TextBox
controls in the entire application create a style for all TextBox
controls:
<Style TargetType="{x:Type TextBox}">
<Setter Property="CharacterCasing" Value="Upper"/>
</Style>

Josh G
- 14,068
- 7
- 62
- 74
-
I know this doesn't solve ALL input casing, but most text input would come in through TextBox controls. – Josh G May 07 '09 at 18:15
-
1I believe it's "TargetType" instead of "DataType": – Wes Aug 02 '12 at 22:54
-
@Wes: Good catch. DataType is used for DataTemplates. Updated. – Josh G Aug 20 '12 at 12:29
1
I recommend creating a custom Textbox class and override an event to automatically capitalize the text. First, this depends on if you want the text to be capitalize as they type or after input is finished.
E.g. for after input is finished
public class AutoCapizalizeTextBox: TextBox
{
public AutoCapitalizeTextBox()
{
}
public AutoCapitlizeTextBox()
{
}
protected override void OnLostFocus(EventArgs e)
{
this.Text = this.Text.ToUpper();
base.OnLostFocus(e);
}
}

Lucas B
- 11,793
- 5
- 37
- 50
0
I don't know if this'll help, it capitalizes all the first letters in the sentence.
http://www.mardymonkey.co.uk/blog/auto-capitalise-a-text-control-in-wpf/

Garrison Neely
- 3,238
- 3
- 27
- 39

Kier
- 1
- 1
-
The link is broken. https://web.archive.org/web/20170210180855/http://www.mardymonkey.co.uk/blog/auto-capitalise-a-text-control-in-wpf/ is the archive of it. – Aaron D May 31 '19 at 22:27
-
This solution just uses a pre-built DLL and doesn't show how to write the code to do it. – Aaron D May 31 '19 at 22:28
0
Perhaps you can use a converter. Here's the code of the converter:
using System;
using System.Globalization;
using System.Windows.Data;
namespace SistemaContable.GUI.WPF.Converters
{
public class CapitalizeFirstLetter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null)
{
string stringToTitleCase = culture.TextInfo.ToTitleCase(value.ToString());
return stringToTitleCase;
}
else
{
return null;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value.ToString();
}
}
}
You need to reference it in a "ResourceDictionary" or in your "App.xaml":
<ResourceDictionary xmlns:converters="clr-namespace:SistemaContable.GUI.WPF.Converters">
<converters:CapitalizeFirstLetter x:Key="CapitalizeFirstLetter"/>
</ResourceDictionary>
And you can use it like this:
<TextBox x:Name="txtNombre" Text="{Binding Usuario.Nombre, Converter={StaticResource CapitalizeFirstLetter}, UpdateSourceTrigger=PropertyChanged}"/>

Gustavo Gavancho
- 25
- 7