There is a set start date that I want to apply for every DatePicker in my app, because repeating the following snippet is not a good idea.
<DatePicker SelectedDate="{Binding Date}"
DisplayDateStart="05/01/2006"/>
As well as setting the default DisplayDateStart in one place I need to be able to bind the SelectedDate property of the DatePicker.
I have tried creating a custom UserControl, but pressing the calendar icon does nothing (well, the text box is flashing but no calendar pop-up appears)
<UserControl x:Class="MyWpfAdd.ModDatePicker"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DockPanel>
DatePicker x:Name="DatePicker" DisplayDateStart="05/01/2006"/>
</DockPanel>
</UserControl>
With underlying code:
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
namespace MyWpfApp
{
public partial class ModDatePicker : UserControl
{
public DateTime? SelectedDate
{
get => (DateTime?)GetValue(SelectedDateProperty);
set
{
this.DatePicker.SelectedDate = value;
SetValue(SelectedDateProperty, value);
}
}
public static readonly DependencyProperty SelectedDateProperty =
DependencyProperty.Register("SelectedDate", typeof(DateTime?), typeof(ModDatePicker), new PropertyMetadata(null));
public DateTime? DisplayDateEnd
{
get => (DateTime?)GetValue(DisplayDateEndProperty);
set
{
this.DatePicker.DisplayDateEnd = value;
SetValue(DisplayDateEndProperty, value);
}
}
public static readonly DependencyProperty DisplayDateEndProperty =
DependencyProperty.Register("DisplayDateEnd", typeof(DateTime?), typeof(ModDatePicker), new PropertyMetadata(null));
public ModDatePicker()
{
InitializeComponent();
}
}
}