tony19
tony19

Reputation: 138326

Binding expression with StringFormat and uppercase Converter?

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}}" />

Upvotes: 1

Views: 6411

Answers (2)

Phil
Phil

Reputation: 42991

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
}

Upvotes: 7

brunnerh
brunnerh

Reputation: 184607

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).

Upvotes: 3

Related Questions