Forcing Data Entered into a WPF DataGrid to UpperCase

All I wanted to do was force data entered directly into a DataGrid into upper case.  You would think this would be an easy thing to do.  Far from it.  It involved delving into things called ValueConverters, which are not that difficult to write but are a major pain in the butt to actually use.

First you need to add a class to your project that implements the IValueConverter interface.  This is a very simple interface with two main methods that you need to be concerned with: Convert and ConvertBack.  Convert takes the raw data and transforms it into the format you want displayed in the grid and ConvertBack takes the value displayed in the grid and transforms it back into the raw data format.  For an upper case converter these methods are pretty simply.  Convert returns the upper case text and ConvertBack just returns the value from the grid, since we want the raw data to be upper case when we are finished.

The entire source code for the class looks like this:


Imports System.Windows.Data

Imports System.Windows.Data

Public Class UpperCaseConverter
Implements IValueConverter

Public Function Convert(ByVal value As Object, _
ByVal targetType As System.Type, _
ByVal parameter As Object, _
ByVal culture As System.Globalization.CultureInfo) As Object _
Implements System.Windows.Data.IValueConverter.Convert

If (value Is Nothing) Then
Return ""
Else
Return value.ToString().ToUpper()
End If
End Function

Public Function ConvertBack(ByVal value As Object, _
ByVal targetType As System.Type, _
ByVal parameter As Object, _
ByVal culture As System.Globalization.CultureInfo) As Object _
Implements System.Windows.Data.IValueConverter.ConvertBack

Return value
End Function

End Class

Pretty simple.

The cumbersome part comes when you try to add the necessary hooks to the XAML to make this work.  First you need to create a resource which references your nifty little value converter.  I put this into app.xaml but you can put it wherever makes the most sense for your application.

First you need to add a namespace, like this:

xmlns:local=”clr-namespace:TransEaz”

where TransEaz would be replaced by the namespace of your application.  Then you need to add your value converter as a resource.  Like this:

Resource XAML

Next you need to add a converter attribute to each DataGridTextColumn that you want to be forced to upper case.  This is a sub-attribute of the Binding attribute.  Your DataGridTextColumn elements will look like this after adding your converter:

DataGrid XAML

In the interest of brevity, I have omitted most of the attributes that aren’t interesting the the context of this discussion.

If you have managed to do all this without any typing mistakes, or putting something in the wrong place in the XAML, not only will you have performed a small miracle, but you should now have a DataGrid whose columns are transformed to upper case as the cursor leaves the column.  This method doesn’t force the text to upper case as it is typed, but only when the user leaves each column.

You would think that there has to be an easier way.

Leave a Reply