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
  • 1 This was a long-standing issue in WPF, where binding defaults to 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
  • I tried the code with the newest MAUI (.net 9) and can not reproduce the issue on macOS – Sir Rufo Commented Nov 22, 2024 at 16:28
Add a comment  | 

2 Answers 2

Reset to default 3

No 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