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

I have a very strange problem:

On my machine, DatePicker changes its watermark and date format to the language / culture that I want to set.

If I copy my application to other computers, do the following:

On some computer, it works the same as on my machine. On other computers, only the date format changes, but there is no watermark! Needless to say, it is very ugly to have a dumper, for example. German date but English watermark.

What is the reason for this behavior?

For i18n, I use the 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(); } } 
+6
source share
3 answers

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

Something that many people (including me) do not like about DatePicker, however, is that by default, if the date is not displayed, it displays the text โ€œSelect dateโ€ as a watermark, and this text is baked into the control - it not localized or available to any public property. This is especially unpleasant if the specified date is optional, and you do not necessarily want to invite your users to select it.

In the same article, he gives a decision on access to Watermark . Here:

How to localize a DatePicker WPF 4.0 control

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

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

You should be based on the current culture, set the text for watermarks, for example. using the example above.

Note: In Silverlight, before Watermark , DatePicker access [ link ]:

 DatePickerTextBox box = base.GetTemplateChild("TextBox") as DatePickerTextBox; box.Watermark = "Type or select a date --> "; 
+5
source

Itโ€™s actually very easy to set a watermark:

 <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/

+3
source

Wayne's solution works fine, but doesn't work when the DatePicker is part of the DataGridColumnHeader, and sometimes when the DatePicker is on the control, which is first hidden and then visible. Matt Hamilton's solution only works onLoad, and when you change selectedDate it is annoying again. Select a date watermark. The easiest solution is to simply override the OnRender event in the user class. If you set the watermark property, not the watermark content, you must also override it in the onload event. The full class is here:

 public class myDateTimePicker : DatePicker { public string Watermark { get; set; } protected override void OnSelectedDateChanged(SelectionChangedEventArgs e) { base.OnSelectedDateChanged(e); //SetWatermark(); } protected override void OnRender(System.Windows.Media.DrawingContext drawingContext) { base.OnRender(drawingContext); SetWatermark(); } private void SetWatermark() { FieldInfo fiTextBox = typeof(DatePicker).GetField("_textBox", BindingFlags.Instance | BindingFlags.NonPublic); if (fiTextBox != null) { DatePickerTextBox dateTextBox = (DatePickerTextBox)fiTextBox.GetValue(this); if (dateTextBox != null) { if (string.IsNullOrWhiteSpace(this.Watermark)) { this.Watermark = "Custom watermark"; } // if you set property this way then you need to override OnSelectedDateChanged event //PropertyInfo piWatermark = typeof(DatePickerTextBox).GetProperty("Watermark", BindingFlags.Instance | BindingFlags.NonPublic); //if (piWatermark != null) //{ // piWatermark.SetValue(dateTextBox, this.Watermark, null); //} var partWatermark = dateTextBox.Template.FindName("PART_Watermark", dateTextBox) as ContentControl; if (partWatermark != null) { partWatermark.Foreground = new SolidColorBrush(Colors.Gray); partWatermark.Content = this.Watermark; } } } } } 
+1
source

Source: https://habr.com/ru/post/953044/


All Articles