1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Cultureで指定した言語を、アプリケーション全体のXAMLでの表記(StringFormat)に反映

Posted at

Cultureで指定した言語を、アプリケーション全体のXAMLでの表記(StringFormat)に反映させる方法です。
.NET 6で確認。

ConverterCultureによる指定(ただし個別)

Text="{Binding Num1, StringFormat='{}{0:C2}', ConverterCulture='ja-JP'}"
public double Num1 { get; set; } = 123456.456;

image.png

ConverterCultureをその都度指定すれば、日本語での通貨表記になります。
ただし、これでは都度設定しないといけません。

アプリケーション全体に設定

Aの部分のコード(下記参照)で、アプリケーション全体のCultureを変更しても、XAMLのBindingで指定するStringFormatは、デフォルトでは次のようになります。

image.png

   <TextBox
       Width="200"
       Height="30"
       Text="{Binding Num1, StringFormat='{}{0:C2}'}" />

次の様に、アプリケーション全体で、XAML上で日本語の表記にするには、Bのコードが必要です。

image.png

public App()
{
    //A
    CultureInfo culture = CultureInfo.CreateSpecificCulture("ja-JP");
    CultureInfo.DefaultThreadCurrentCulture = culture;
    CultureInfo.DefaultThreadCurrentUICulture = culture;
    Thread.CurrentThread.CurrentCulture = culture;
    Thread.CurrentThread.CurrentUICulture = culture;
    //B XAMLでの表示を変更
    XmlLanguage language = XmlLanguage.GetLanguage(culture.IetfLanguageTag);
    FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(language));
}

参照

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?