admin管理员组文章数量:1122833
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?
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?
Share Improve this question edited Nov 22, 2024 at 11:11 Rafael asked Nov 22, 2024 at 10:57 RafaelRafael 1,9112 gold badges22 silver badges58 bronze badges 2 |2 Answers
Reset to default 3No there isn't way to specify custom culture through XAML bindings.
But, Assuming your app culture is set to English, since it's displaying Su, You can create a converter to convert the string with custom culture.
public class DateToLocalizedStringConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is DateTime date)
{
var germanCulture = new CultureInfo("de-DE");
return date.ToString("ddd dd.MM.yy", germanCulture);
}
return value;
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
and use it on the Span like below
<Label>
<Label.FormattedText>
<FormattedString>
<Span Text="{Binding StartDate,Converter={StaticResource DateToLocalizedStringConverter},StringFormat='{0:ddd}'}" FontAttributes="Bold" />
<Span Text=" " />
<Span Text="{Binding StartDate, StringFormat='{0:dd.MM.yy}'}" />
</FormattedString>
</Label.FormattedText>
</Label>
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" />
本文标签: mauiFormat DateTime in XAML using custom CultureStack Overflow
版权声明:本文标题:maui - Format DateTime in XAML using custom Culture - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736304237a1932165.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
en-US
(because nothing exists outside of North America). It's a shame to see that decision has been carried over to MAUI. – Richard Deeming Commented Nov 22, 2024 at 11:18