It seems that the DisplayDate doesn't bind correctly when the DatePicker is inside a ControlTemplate.
This is how I bind the DisplayDate to the DatePicker through MVVM. But the DatePicker wont display the CustomDisplayDate correctly. It keep show the default display date which is DateTime.Now().
Keep in mind that this code does work if the DatePicker is not inside a ControlTemplate.
Any suggestions on how to solve this?
XAML code:
<Window.Resources>
<ControlTemplate x:Key="DateField">
<StackPanel>
<TextBlock Text="Custom Display Date:" />
<TextBlock Text="{Binding CustomDisplayDate}"/>
<DatePicker
SelectedDate="{Binding CustomSelectedDate}"
DisplayDate="{Binding CustomDisplayDate}"
HorizontalAlignment="Left"
VerticalAlignment="Top"/>
</StackPanel>
</ControlTemplate>
</Window.Resources>
<Grid>
<ContentControl Template="{StaticResource DateField}" />
</Grid>
and the ViewModel:
public class MainWindowViewModel
{
public MainWindowViewModel()
{
CustomDisplayDate = DateTime.Now.AddDays(5);
}
public DateTime? CustomSelectedDate { get; set; }
public DateTime CustomDisplayDate { get; set; }
}