Question

I have a TextBlock whose text is bound to live ticker (via System.Date.Now) and that binding contains a ValueConverter that is supposed to convert the string to uppercase. However, the string does not result in uppercase letters (the result is as if the converter weren't even there). How do I get the binding result to be uppercase?

<TextBlock 
        Text="{Binding Now,
                       Source={StaticResource ticker},  
                       StringFormat={}{0:dddd\, MMMM d}, 
                       Converter={StaticResource CaseConverter}}" />
Was it helpful?

Solution

H.B. is correct. You need a better converter:

    <TextBlock Text="{Binding Now, Source={StaticResource ticker}, Converter={StaticResource UpperCaseDateConverter}, ConverterParameter='dddd, MMMM d'}" />

Converter:

public class UpperCaseDateConverter : IValueConverter
{
    #region Implementation of IValueConverter

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((DateTime)value).ToString((string)parameter).ToUpperInvariant();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

OTHER TIPS

The converter should be applied before the StringFormat, so if the StringFormat destroys it again you might want to move that formatting logic into the converter (or apply multiple converters using a group-converter of sorts).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top