In my .NET MAUI application I want to format date/time in XAML to show weekday short-name along with the date. For example, date 24.11.2024 should be displayed like that (using German weekday abbreviation for Sonntag
as So
):
So 24.11.24
For this approach:
<Label>
<Label.FormattedText>
<FormattedString>
<Span Text="{Binding StartDate, StringFormat='{0:ddd}'}" FontAttributes="Bold" />
<Span Text=" " />
<Span Text="{Binding StartDate, StringFormat='{0:dd.MM.yy}'}" />
</FormattedString>
</Label.FormattedText>
</Label>
Date is displayed like that:
Su 24.11.24
I.e it uses English name abbreviation (Sunday).
My question, is there a way to specify custom culture upon converting date in XAML?
If you want to react to CultureInfo.CurrentCulture
changes which is used to control numerical and date formats, you can add it to your view model as follows:
public MyViewModel : ObservableObject
{
public CultureInfo Culture
{
get => CultureInfo.CurrentUICulture;
set
{
CultureInfo.CurrentUICulture = value; // for resource strings
CultureInfo.CurrentCulture = value; // for date and number formats
OnPropertyChanged(nameof(Culture));
}
}
[RelayCommand]
public void SetCultureName(string cultureName)
=> Culture = new CultureInfo(cultureName);
}
Doing so will allow your app to property bind to cultural changes. You can use the following MultiBinding trick to update your date formats accordingly. The trick works because when Culture
changes; the MultiBinding
forces an update to the text, and we are also changing CultureInfo.CurrentCulture
the default string.Format
functions will take the new culture into account.
<Label>
<Label.FormattedText>
<FormattedString>
<Span Text="{MultiBinding {Binding StartDate},
{Binding Culture},
StringFormat='{0:ddd}'}"
FontAttributes="Bold" />
<Span Text=" " />
<Span Text="{MultiBinding {Binding StartDate},
{Binding Culture},
StringFormat='{0:dd.MM.yy}'}" />
</FormattedString>
</Label.FormattedText>
</Label>
<Button Text="de-DE"
Command="{Binding SetCultureNameCommand}"
CommandParameter="de-DE" />
<Button Text="en-US"
Command="{Binding SetCultureNameCommand}"
CommandParameter="en-US" />