String format using UWP and x: Bind

Does anyone know how to format the date when using x: Bind in a Windows 10 UWP application?

I have a TextBlock associated (x: Bind) with the DateTime property on my ViewModel that is being read from SQL. I want to format the output to "dd / MM / yyy HH: mm (ddd)". Is there an easy way to do this?

The default format is "dd / MM / yyy HH: mm: ss", which I assume comes from the default. Is it possible to replace it?

Thanks.

+9
c # uwp xaml
source share
2 answers

Use StringFormatConverter (check, maybe you are using some library that already includes it, for example, the UWP Toolkit (thanks, @maxp) or the older Cimbalino Toolkit ):

 public class StringFormatConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { if (value == null) return null; if (parameter == null) return value; return string.Format((string)parameter, value); } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } } 

add it to your resource

 <Page.Resources> <converters:StringFormatConverter x:Key="StringFormatConverter" /> </Page.Resources> 

and use it like that

 <TextBlock Text="{x:Bind Text, Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0:dd/MM/yyy HH\\\\:mm (ddd)}'}" /> 
+21
source share

you can use

 {x:Bind ViewModel.DateTimeProperty.ToString("....")} 
+7
source share

All Articles