Search code examples
c#wpfglobalization

How to set the DateFormat (Language) in a textbox?


I have a WPF application where I am dynamic creating the XAML which includes a textbox to display the current DATE and I want to show it in FRENCH (because it includes the day of the week).

In the XAML itself this is easy:

<TextBox Name="OrderDateText"
    Text="{Binding Path=OrderDate, StringFormat=dddd: dd-MM-yyyy}"
    xml:lang="fr-CA"

However when done in CODE I cannot seem to figure out how to set the language:

TextBox txtboxOrderdDate = new TextBox();
txtboxOrderdDate.Language = ???????????????????
{
   StringFormat = "dddd: dd-MM-yyyy"
};
txtboxOrderdDate.SetBinding(TextBox.TextProperty, binding);

Solution

  • Use XMLLanguage from System.Windows.Markup namespace:

    txtboxOrderdDate.Language = XmlLanguage.GetLanguage("fr-CA");
    

    Note that based on your Xaml, you should set StringFormat in your Binding, not in Language.

    txtboxOrderdDate.SetBinding(TextBox.TextProperty, new Binding()
    {
        StringFormat = "some format here",
        OtherProps = ...
    });