10

i playing around with the new Windows Store Universal App template which could be used for Windows 8.1 and Windows Phone 8.1 and was wondering how to format strings in XAML code.

What i tried (XAML):

 <TextBlock Text="{Binding TestItem.CurrentDate, StringFormat={}{0:MM/dd/yyyy}}" /> 

The problem is that StringFormat is not available in Windows.UI.Xaml.Controls.TextBox.

Microsoft has created a sample project which is all about formatting dates. But the approach used there is based on (ugly) code behind.

So, here is my Question:

  • Why is StringFormat not available in Windows Store Universal Apps?
  • How do i format strings using XAML Code only?


EDIT: I decided to go with the converter solution, for those of interest here is the code:

public class DateConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { if (value == null) return null; if (!(value is DateTime)) return null; var dateTime = (DateTime)value; var dateTimeFormatter = new DateTimeFormatter(YearFormat.Full, MonthFormat.Full, DayFormat.Default, DayOfWeekFormat.None, HourFormat.None, MinuteFormat.None, SecondFormat.None, new[] { "de-DE" }, "DE", CalendarIdentifiers.Gregorian, ClockIdentifiers.TwentyFourHour); return dateTimeFormatter.Format(dateTime); } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } } 

I'm happy for every single advice how to improve the code above, feel free to comment.

Thank you Mikael Dúi Bolinder and Martin Suchan for your suggestion/answer.

6
  • 1
    Maybe you should create a converter? Commented Apr 12, 2014 at 12:11
  • Yes, that would be an option but i don't want to do this before i now that there is no other (better) way to do it. Commented Apr 12, 2014 at 12:12
  • 1
    How did you link the converter in the markup? All my combinations of STaticResource, Relative.. etc all fail. On a side note: I find it ridiculous that StringFormat would be missing. It's 2014 & .NET 4.5. Of all the meetings they had over Windows Phone 8.1, nobody brought up formatting of dates on DataBound controls? Commented Jun 4, 2014 at 5:05
  • @JohnK: soo true, I don't understand Microsoft anymore. I recently started to loose faith. Commented Jun 4, 2014 at 8:11
  • Agree 100%, the more I write code for WP8.1 the more I also doubt they know what they are doing. I've wasted hours trying to do something I thought would take seconds in the form of .ToString("dd/mm/yyyy"), I've now given up and now waiting for help from this post. Smh! Commented Jun 4, 2014 at 8:53

1 Answer 1

7

DataBinding in Windows Runtime project types does not support StringFormat property, the options you have are:

  • Use already formatted date as a get-only property in your ViewModel.
  • Use Converter - you can even create StringFormatConverter where you can pass the DateTime format as ConverterParameter. Here's a solution how such StringFormatConverter could work.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.