WPF Converter: CaseConverter

Posted by Jon Bullen on 2010-08-29
Estimated Reading Time 1 Minutes
Words 176 In Total

Case Converter was made when I wanted the text to display in a WPF UI in upper case. Probably not the best class name, but anyway. To get the code and install read on. Make any UI string uppercase. Once you have registered the resource and created the class just use the converter in the element you want converted.

1
<TextBlock Text="{Binding Title, Converter={StaticResource CaseConverter}}"/>

Add the following element to the XAML, usually in Window.Resources and ensure that you have registered your namespace for local to the namespace of your application. e.g. xmlns:local=”clr-namespace:ExampleApp”

1
<local:CaseConverter x:Key="CaseConverter" />

This is the class, short and sweet.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class CaseConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string text = value as string;
if(text != null)
{
return text.ToUpper(culture);
}

return value;
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
}

If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you!