WPF DatePicker IsEnabled property not changing appearance

Jonathan picture Jonathan · Apr 14, 2010 · Viewed 8.5k times · Source

I think I have found an issue with the DatePicker in the toolkit, perhaps some of you gurus can check it out.

The issue is when setting the IsEnabled property of the DatePicker. If set in XAML, it stays grey even if you set the IsEnabled to true at run time. The same goes for the other way around should it start off being enabled.

The button just changes the IsEnabled property of the date picker, you will see that when it becomes enabled, the style remains grayed out.

<Window x:Class="WpfApplication3.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:tk="http://schemas.microsoft.com/wpf/2008/toolkit"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <tk:DatePicker x:Name="txtDate" IsEnabled="False"></tk:DatePicker>
        <Button Height="25" Click="Button_Click"></Button>
    </StackPanel>
</Window>

private void Button_Click(object sender, RoutedEventArgs e)
{
    txtDate.IsEnabled = !txtDate.IsEnabled;
}

Answer

David picture David · Mar 14, 2011

I solved this by explicitly setting DatePicker's visual style in IsEnabledChanged handler:

private void datePicker_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    DatePicker datePicker = sender as DatePicker;
    if (datePicker.IsEnabled)
        VisualStateManager.GoToState(datePicker, "Normal", true);
    else
        VisualStateManager.GoToState(datePicker, "Disabled", true);
}