How to use StringFormat in XAML elements?

XamlString Formatting

Xaml Problem Overview


I'm deep in a XAML stack of elements binding to orders.

The order date displays as e.g. "12/31/2008 12:00:00 AM".

I want it to display as e.g. "31.12.2008".

How can I do this? I have seen other stackoverflow questions mention StringFormat but they use multibinding in ways that I can't get to work.

Here is the kind of syntax I would like (this is pseudocode), simply specifying StringFormat where you need it, is this possible somehow?

<StackPanel>
    <ListView ItemsSource="{Binding Orders}">
        <ListView.View>
            <GridView>
                <GridViewColumn 
                    Header="Order ID" 
                    DisplayMemberBinding="{Binding Path=OrderID}"
                    StringFormat="{}{1:dd.MM.yyyy}"/>
                <GridViewColumn 
                    Header="Order Date" 
                    DisplayMemberBinding="{Binding Path=OrderDate}"/>
            </GridView>
        </ListView.View>
    </ListView>
</StackPanel>

Xaml Solutions


Solution 1 - Xaml

I haven't tested it, but I think this should work:

<GridViewColumn
    Header="Order Date"
    DisplayMemberBinding=
        "{Binding Path=OrderDate, StringFormat='{}{0:dd.MM.yyyy}'}"/>

Solution 2 - Xaml

In general, you can look for an associated *StringFormat dependency property. For example, all ContentControl implementations (such as Label and Tooltip) have the ContentStringFormat dependency property:

<Label
    Content="{Binding Path=DateAsked}"
    ContentStringFormat="{}{0:yyyy/MM/dd HH:mm:ss}" />

In your case, while the GridViewColumn has the HeaderStringFormat dependency property to go along with Header, there is no analog for the DisplayMemberBinding and so you will need .NET 3.5 SP1 (get it with Visual Studio 2008 SP1) or better to use the new BindingBase.StringFormat Property:

<GridViewColumn 
    Header="Order ID"
    DisplayMemberBinding="{Binding Path=OrderID, StringFormat='{}{0:dd.MM.yyyy}'}"
/>

There are lots more examples at the blog post Trying out Binding.StringFormat.

Solution 3 - Xaml

XAML

<UserControl.Resources>
    <myNamespace:DateTimeConverter x:Key="DateTimeConverter" />
</UserControl.Resources>

<GridViewColumn 
DisplayMemberBinding=="{Binding Path=OrderDate, Converter={StaticResource DateTimeConverter}}" 
/>

C#

public class DateTimeConverter : IValueConverter
{
    public object Convert(object value,
                       Type targetType,
                       object parameter,
                       CultureInfo culture)
    {
        if (value != null)
        {
            return ((DateTime)value).ToString("dd.MM.yyyy");
        }
        else
        {
            return String.Empty;
        }
    }

    public object ConvertBack(object value,
                              Type targetType,
                              object parameter,
                              CultureInfo culture)
    {
        return DateTime.Parse(value.ToString());
    }
}

Solution 4 - Xaml

If you wanted to localize the date format, you can include it in your .resx file. You will have to set up your app for localization by following this guide: https://developer.xamarin.com/guides/xamarin-forms/advanced/localization/.

The resx entry:

<data name="DateFormat" xml:space="preserve">
    <value>{0:dddd d MMMM H&#x3a;mm}</value>
</data>

In your content page, you then include the location of the resx file

xmlns:il8n="clr-namespace:MyProject.Localization;assembly=MyProject"

And then you can use it in your binding like so:

<Label
    Text = "{Binding Time, StringFormat={il8n:Translate DateFormat}}"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEdward TanguayView Question on Stackoverflow
Solution 1 - XamlRazzieView Answer on Stackoverflow
Solution 2 - XamlOlivier DagenaisView Answer on Stackoverflow
Solution 3 - XamlsipsorceryView Answer on Stackoverflow
Solution 4 - XamlRobVoiseyView Answer on Stackoverflow