I have taken Matt's idea, and extended it somewhat; I've implemented an Attached Behaviour which allows you to define the watermark for each DatePicker control through XAML. Here is the code:
namespace DatePickerWatermark
{
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
public static class DatePickerWatermarkBehaviour
{
public static readonly DependencyProperty WatermarkProperty =
DependencyProperty.RegisterAttached(
"Watermark",
typeof(string),
typeof(DatePickerWatermarkBehaviour),
new UIPropertyMetadata(null, OnWatermarkChanged));
public static string GetWatermark(Control control)
{
return (string)control.GetValue(WatermarkProperty);
}
public static void SetWatermark(Control control, string value)
{
control.SetValue(WatermarkProperty, value);
}
private static void OnWatermarkChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
var datePicker = dependencyObject as DatePicker;
if (datePicker == null)
return;
if ((e.NewValue != null) && (e.OldValue == null))
datePicker.Loaded += DatePickerLoaded;
else if ((e.NewValue == null) && (e.OldValue != null))
datePicker.Loaded -= DatePickerLoaded;
}
private static void DatePickerLoaded(object sender, RoutedEventArgs e)
{
var datePicker = sender as DatePicker;
if (datePicker == null)
return;
var datePickerTextBox = GetFirstChildOfType<DatePickerTextBox>(datePicker);
if (datePickerTextBox == null)
return;
var partWatermark = datePickerTextBox.Template.FindName("PART_Watermark", datePickerTextBox) as ContentControl;
if (partWatermark == null)
return;
partWatermark.Content = GetWatermark(datePicker);
}
private static T GetFirstChildOfType<T>(DependencyObject dependencyObject) where T : DependencyObject
{
if (dependencyObject == null)
return null;
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject); i++)
{
var child = VisualTreeHelper.GetChild(dependencyObject, i);
var result = (child as T) ?? GetFirstChildOfType<T>(child);
if (result != null)
return result;
}
return null;
}
}
}
Wayne, this works great but does not work when DatePicker is part of DataGridColumnHeader and sometimes when DatePicker is on control that is first hidden and then visible. Matt Hamilton's solution works only onLoad, and when you change selectedDate there is again annoying Select a date watermark. The easiest solution is just to override OnRender event in custom class. If you set watermark property and not a watermark content that you need to override onload event as well. Complete 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;
}
}
}
}
}
I have taken Matt's idea, and extended it somewhat; I've implemented an Attached Behaviour which allows you to define the watermark for each DatePicker control through XAML. Here is the code:
Here's some example XAML:
Unfortunately, setting the watermark text is a bit tricky.
How it can be done you can see in this article: Changing the watermark text in a DatePicker control
Wayne, this works great but does not work when DatePicker is part of DataGridColumnHeader and sometimes when DatePicker is on control that is first hidden and then visible. Matt Hamilton's solution works only onLoad, and when you change selectedDate there is again annoying Select a date watermark. The easiest solution is just to override OnRender event in custom class. If you set watermark property and not a watermark content that you need to override onload event as well. Complete class is here:
Here's how to do it without having to derive a new control from DatePicker:
Customizing the WPF DatePicker's Watermark