Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a WPF application on the dotnet core 7 platform and in the App class, in the OnStartup event, I have changed the Culture as follows: When I use DatePicker, when I click on the date selection button, it shows me the Persian calendar correctly. However, when I select a date, it displays the Gregorian date in Persian numbers. When I click on the year at the top of the page to select the date, it shows me the Gregorian months in Persian. Then, when I click again, it shows me the Gregorian years. What should I do to solve this problem? I have attached a sample image of the program running.

problem image

What I have tried:

<pre>protected override void OnStartup(StartupEventArgs e)
{
    CultureInfo cultureInfo = new CultureInfo("fa-IR");
    Thread.CurrentThread.CurrentCulture = cultureInfo;
    Thread.CurrentThread.CurrentUICulture = cultureInfo;

    FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
}
---
...
<StackPanel FlowDirection="RightToLeft" VerticalAlignment="Center" Margin="10,0,10,0" Height="209">
    <DatePicker />
...
Posted

1 solution

Localization with WPF require you to set the text to be used as there are no language translations by default. Here is a search show a number of ways of doing it: wpf mulitiple language support - Google Search[^] and here is an article on CP that demonstates one way: Simplest Way to Implement Multilingual WPF Application[^]

Now, regarding your issue with the DatePicker control. There are two parts:
1. The calendar month and the abbreviated day-of-week names - you have a solution already - simply set the culture.

2. Altering the TextBox text message to a localize message.
You have 2 choices:
a) extract and create your own template - not ideal
b) to progmatically change the text with your own - this is the method that I will demonstrate below.

Here is a working solution...

XAML
XML
<DatePicker x:Name="MyDatePicker"
            Loaded="MyDatePicker_OnLoaded"
            HorizontalAlignment="Center"
            VerticalAlignment="Top"
            Width="200"/>

Code-Behind:
C#
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        CultureInfo cultureInfo = new CultureInfo("fa-IR");
        Thread.CurrentThread.CurrentCulture = cultureInfo;
        Thread.CurrentThread.CurrentUICulture = cultureInfo;

    }


    private void MyDatePicker_OnLoaded(object sender, RoutedEventArgs e)
    {
        DatePicker datePicker = sender as DatePicker;
        if (datePicker is null)
            return;

        DatePickerTextBox pickerTextBox =
            datePicker.GetChildOfType<DatePickerTextBox>();

        if (pickerTextBox is null)
            return;

        ContentControl contentControl = pickerTextBox
            .Template
            .FindName("PART_Watermark", pickerTextBox) as ContentControl;

        if (contentControl is null)
            return;

        contentControl.Content = "تاریخ را انتخاب کنید";
    }
}

Extension Method:
C#
public static class DependencyObjectExtensions
{
    public static T GetChildOfType<T>(this DependencyObject depObj) 
        where T : DependencyObject
    {
        if (depObj == null) return null;

        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            var child = VisualTreeHelper.GetChild(depObj, i);

            var result = (child as T) ?? GetChildOfType<T>(child);
            if (result != null) return result;
        }
        return null;
    }
}

I have hard-coded the text in the example. If you're only using one language, this is fine. If not, refer to the links at the beginning of this solution.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900