Question

I'm trying to display a date in a WPF TextBlock in the following format:

09:35 Uhr

My first try of course didn't work:

<TextBlock Text="{Binding StartTime,StringFormat={}{0:hh:mm Uhr}}"

resulted in the string 09:35 U9r since the h in the string after the date is interpreted as aplaceholder for the hour. Writing the h as &#104; also does not work, neither does trying to escape it as {{h}}.

So, it it actually possible to achieve what I want to do? Or would I have to break up the string into two TextBlocks, use a custom converter or something like that?

Was it helpful?

Solution 2

Use \ according to http://msdn.microsoft.com/en-us/library/aa719856(v=vs.71).aspx

<TextBlock Text="{Binding StartTime,StringFormat={}{0:hh:mm U\\hr}}"

Alternatively you can also use a IValueConverter something like

public class MyFormatter : IValueConverter
{
  public object Convert(object value, Type tagetType, object parameter, CultureInfo culture)
  {
     return string.Format("{0:hh:mm}", value)+" Uhr";
  }
...
//Just throw NotImplemented for ConvertBack
}

OTHER TIPS

Try this:

<TextBlock Text="{Binding Path=StartTime, StringFormat={}{0:hh:mm} Uhr}" ... />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top