Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DatePicker Watermark uses wrong Language, but the Dateformat is correct

I have a very strange Problem:

On my machine the DatePicker changes its watermark AND date-format according to the language/culture i want to set.

If i copy my application to other computers following happens:

On some computer it works like it does on my machine. On other computers only the date-format changes but the watermark does not! Needless to say it is very ugly to have a datepicker with e.g. a german date but an english watermark.

What is the cause for that behaviour?

For i18n i use following code:

App.xaml.cs:

public partial class App : Application
{
    public App()
    {
        CultureInfo ci = new CultureInfo("de-DE"); 
        Thread.CurrentThread.CurrentCulture = ci;
        Thread.CurrentThread.CurrentUICulture = ci;
    }
}

WindowMain.xaml.cs:

public partial class WindowMain : RibbonWindow
{
    public WindowMain()
    {
        this.Language = XmlLanguage.GetLanguage("de-DE");
        this.InitializeComponent();
    }
}
like image 677
M C Avatar asked Sep 03 '13 10:09

M C


2 Answers

It's actually really easy to set the watermarked text:

<DatePicker>
  <DatePicker.Resources>
    <Style TargetType="DatePickerTextBox">
      <Setter Property="Text" Value="Watermark Text"/>
    </Style>
  </DatePicker.Resources>
</DatePicker>

http://www.admindiaries.com/change-datepicker-watermark-in-wpf/

like image 197
Jón Arnar Avatar answered Sep 29 '22 07:09

Jón Arnar


One thing I can say, Watermark in DatePicker implemented buggy, there is no easy access. Perhaps, because of this difficulty, the localization of the text does not work. There is a wonderful article of @Matt Hamilton, quote from here:

Something that a lot of people (myself included) don't like about the DatePicker, though, is that by default if no date is displayed it shows the text "Select a date" as a watermark, and this text is baked into the control - it's not localized or accessible by any public property. This is particularly frustrating if the date in question is optional and you don't necessarily want to prompt your users to select one.

In the same article, he provide the decision of how to access to Watermark. Here:

How to localize the WPF 4.0 DatePicker control

@Wayne Maurer created a universal solution in the form of attached dependency property:

<DatePicker Grid.Row="2" 
            local:DatePickerWatermarkBehaviour.Watermark="Select the date" />

You need to be based on the current culture, set the text for watermarks, e.g. using above example.

Note: In Silverlight to Watermark in DatePicker made access [link]:

DatePickerTextBox box = base.GetTemplateChild("TextBox") as DatePickerTextBox;
box.Watermark = "Type or select a date --> "; 
like image 32
Anatoliy Nikolaev Avatar answered Sep 29 '22 06:09

Anatoliy Nikolaev