1

I have a data template with a TexBlock in XAML. This TexBlock shows a word in a word list. Every word I want to put the first letter capitalized, because all words are in lowercase.

<phone:PhoneApplicationPage.Resources>
        <DataTemplate x:Key="AddrBookItemTemplate">
            <StackPanel VerticalAlignment="Top">
                <TextBlock Margin="5,0,0,0" FontSize="20" Text="{Binding name}" />
            </StackPanel>
        </DataTemplate>
 </phone:PhoneApplicationPage.Resources>

In c# implement the converter

namespace Converter.ViewModels

{

    public class ToCapitalizeConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                return char.ToUpper(value.ToString()[0]) + value.ToString().Substring(1);
            }

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

In App.xaml

... xmlns:vm="clr-namespace:Converter.ViewModels"

   <Application.Resources>
          <vm:ToCapitalizeConverter x:Key="ToCapitalizeConverter"/>    
    </Application>

In MainPage.xaml

<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Key="AddrBookItemTemplate">
                <StackPanel VerticalAlignment="Top">
                    <TextBlock Margin="5,0,0,0" FontSize="20" Text="{Binding name, Converter={StaticResource ToCapitalizeConverter}}" />                 
                </StackPanel>
           </DataTemplate>
     </phone:PhoneApplicationPage.Resources>
EduardoUstarez
  • 603
  • 11
  • 22
  • possible duplicate of [WPF/XAML: how to make all text upper case / capital?](http://stackoverflow.com/questions/1762485/wpf-xaml-how-to-make-all-text-upper-case-capital) – har07 May 05 '14 at 03:49
  • I'll change the question because I did not explain the question clearly – EduardoUstarez May 06 '14 at 02:28

1 Answers1

1

You can use a converter as follows:

<TextBlock Margin="5,0,0,0" FontSize="20" Text="{Binding name, Converter ={StaticResource myConverter}}" />

Specific information on how to implement a converter can be found here. You can essentially perform any operation you like on the text. I actually like Humanizer to do these type of text conversions.

poppastring
  • 317
  • 1
  • 9