String format using UWP and x:Bind
Asked Answered
T

4

17

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

I have a TextBlock that is bound (x:Bind) to a DateTime property on my ViewModel which is read from SQL. I want to format the output to "dd/MM/yyy HH:mm (ddd)". Is there a simple way of doing this?

The default format is "dd/MM/yyy HH:mm:ss" which I presume is coming from a default. Could this be replaced maybe?

Thanks.

Thormora answered 1/12, 2015 at 17:25 Comment(0)
T
28

Use a StringFormatConverter (check if you maybe use some library, which already includes it, e.g. 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 page resource

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

and use it like this

<TextBlock Text="{x:Bind Text, Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0:dd/MM/yyy HH\\\\:mm (ddd)}'}" />
Touraco answered 1/12, 2015 at 17:36 Comment(2)
Perfect. Thank you very much sibbl. Works a treat.Thormora
I've just noticed this is also part of the UWP Toolkit: learn.microsoft.com/en-us/windows/communitytoolkit/helpers/…Overeat
E
12

you can use

{x:Bind ViewModel.DateTimeProperty.ToString("....")}
Ephebe answered 28/4, 2017 at 14:52 Comment(3)
You sure can, as long as you target Creators UpdateYelena
In my case is not working because the compiler interprets that I'm calling to the ToString(IFormatProvider) overload, so I'd to call the (string, IFormatParameter) overload like this: {x:Bind ViewModel.DateTimeProperty.ToString('...', {x:Null})}Ayrshire
The comment of @Ayrshire should be the answer to save people a lot of time.Compete
D
5

Formated DateTime

Function binding is much better approach than classic Converter:

 <TextBlock Text="{x:Bind DateTimeToString(MyDateTime,'dd/MM/yyy HH\\\\:mm (ddd)')}" />

Code behind (it could be placed in separate class):

//"Converter"
public string DateTimeToString(DateTime dateTime, string format) => dateTime.ToString(format);

public DateTime MyDateTime { get; set; } = DateTime.Now;

Why is it better than classic converter?

  • Shorter -> no boil plait code
  • Strongly typed -> Detects exceptions in build time.
Dynamo answered 8/7, 2020 at 12:48 Comment(0)
D
2

similar to the code behind

xmlns:globalization="using:System.Globalization"
...

{x:Bind ViewModel.DateTimeProperty.ToString('dd/MM/yyy HH:mm (ddd)', globalization:DateTimeFormatInfo.InvariantInfo)}

...

Decrial answered 24/6, 2021 at 21:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.