2

I am working with a DatePicker in a WPF application. I want to change the date format of the DatePicker to the system date format.

This is the mail window:

<Window x:Class="DateTimeDemo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <Button x:Name="Btn" Width="100" Click="Btn_Click">Click</Button>
    </StackPanel>
</Window>

The child window is opened using the following code:

private void Btn_Click(object sender, RoutedEventArgs e)
{
    ChiildWindow Cw = new ChiildWindow();
    Cw.ChildDatePicker.SelectedDate = DateTime.Now;
    Cw.ShowDialog();
    Cw.Focus();
}

This is the child window, which only contains a DatePicker:

<Window x:Class="DateTimeDemo.ChiildWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ChiildWindow" Height="300" Width="300">
    <StackPanel>
        <DatePicker x:Name="ChildDatePicker"/>
    </StackPanel>
</Window>

When I open the child window the format is taken from the system culture but if I change the system date format from the control panel after the window opens the format doesn't change.

I have tried the following code in the ChildWindow.xaml.cs file:

Thread.CurrentThread.CurrentCulture = CultureInfo.CurrentCulture;

But when I open the child window the format doesn't change.

Does anyone know what the problem is and how I can solve this issue?

Tharwen
  • 3,057
  • 2
  • 24
  • 36
Arijit
  • 93
  • 3
  • 13
  • what exactly your question is? have you solved the issue by assigning the CurrentCulture in code behind? – gaurawerma May 23 '12 at 09:02
  • No I have Written that The Problem is not yet Solved..I have Tried those things but not yet solved.Can You Please Help me?? – Arijit May 24 '12 at 12:14

1 Answers1

1

According to this answer ( How to get current regional settings in C#? ), you can detect culture using this code :

private class State
{
    public CultureInfo Result { get; set; }
}

public CultureInfo GetCurrentCulture()
{
    Thread.CurrentThread.CurrentCulture.ClearCachedData();
    var thread = new Thread(s => ((State)s).Result = Thread.CurrentThread.CurrentCulture);
    var state = new State();
    thread.Start(state);
    thread.Join();
    var culture = state.Result;
}

Finally, you need to set the CurrentUICulture property to affect UI components :

Thread.CurrentThread.CurrentUICulture = GetCurrentCulture();
Community
  • 1
  • 1
mathieu
  • 30,974
  • 4
  • 64
  • 90