How to use StringFormat in XAML elements?

I am deep in the stack of XAML order binding elements.

The order date is displayed, for example. 12/31/2008 12:00:00 a.m.

I want it to be displayed as, for example, "December 31, 2008".

How can i do this? I have seen the https://stackoverflow.com/a/166269/9129/9129/index.html mention StringFormat, but they use multi-binding in ways that I cannot get to work.

Here is a syntax that I would like (this is pseudo-code), just specifying StringFormat where you need it, is it 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> 
+49
xaml string-formatting
Mar 26 '09 at 13:19
source share
4 answers

I have not tested it, but I think this should work:

 <GridViewColumn Header="Order Date" DisplayMemberBinding= "{Binding Path=OrderDate, StringFormat='{}{0:dd.MM.yyyy}'}"/> 
+72
Mar 26 '09 at 13:31
source share

In general, you can find the related dependency property *StringFormat . For example, all ContentControl implementations (such as Label and Tooltip) have a ContentStringFormat dependency property :

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

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

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

There are many more examples of the Binding.StringFormat attempt on the blog .

+28
Sep 25 '10 at 15:09
source share

Xaml

 <UserControl.Resources> <myNamespace:DateTimeConverter x:Key="DateTimeConverter" /> </UserControl.Resources> <GridViewColumn DisplayMemberBinding=="{Binding Path=OrderDate, Converter={StaticResource DateTimeConverter}}" /> 

FROM#

 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()); } } 
+3
Mar 26 '09 at 13:24
source share

If you want to localize the date format, you can include it in a .resx file. You will need to configure the application for localization by following this guide: https://developer.xamarin.com/guides/xamarin-forms/advanced/localization/ .

Resx entry:

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

On your content page then specify the location of the resx file

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

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

 <Label Text = "{Binding Time, StringFormat={il8n:Translate DateFormat}}" 
0
Jul 25 '17 at 9:04 on
source share



All Articles