WPF DatePicker IsEnabled property does not change appearance

I think I found a problem with DatePicker in the toolbox, maybe some of you gurus can check this out.

The problem is setting the IsEnabled property for DatePicker . If it is set to XAML, it remains gray, even if, at run time, set IsEnabled to true. The same thing happens on the other hand, if it starts to work.

The button simply changes the IsEnabled property to select a date, you will see that when it becomes enabled, the style will remain gray.

 <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; } 
+6
styles wpf toolkit datepicker isenabled
source share
5 answers

I solved this by explicitly setting the DatePicker visual style in the 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); } 
+7
source share

Want some good news or bad news?

The good news is that Microsoft claims that this problem was fixed in the February 2010 release of WPFToolkit. And it was.

The bad news is that while setting the DatePicker IsEnabled to True will allow you to use DatePicker, you can now click the Calendar button to select a date, it will still be disabled.

"Mistake"? Did I say the word "mistake"?

Of course not.

You can get around this by applying <Style> though.

Below is a simple xaml code for demonstration.

It displays two rows, each of which contains CheckBox and DatePicker. When you click on a CheckBox in a line, it should include a DatePicker in that line.

This shows the difference between a DatePicker with no style (in the first line) and a DatePicker with style (in the second line).

alt text

Both DatePickers are correctly enabled / disabled, but in the first line it never looks like it is. DatePicker in the second line uses Style to show the user when he is disconnected.

Notice how this code sets the background of both the DatePicker control and the DatePickerTextBox part.

 <Window x:Class="WPFDatePickerTest.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wpf="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit" xmlns:primitives="clr-namespace:Microsoft.Windows.Controls.Primitives;assembly=WPFToolkit" Title="Window1" Height="317" Width="461"> <Window.Resources> <Style TargetType="{x:Type primitives:DatePickerTextBox}"> <Style.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Background" Value="Transparent"/> </Trigger> </Style.Triggers> </Style> <Style x:Key="DatePickerStyle1" TargetType="{x:Type wpf:DatePicker}"> <Style.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Background" Value="{x:Static SystemColors.InactiveBorderBrush}"/> </Trigger> </Style.Triggers> </Style> </Window.Resources> <Grid> <StackPanel> <WrapPanel> <CheckBox Height="16" Name="cbDateOfJoining" Width="120">Date of joining</CheckBox> <wpf:DatePicker Height="25" Name="datePicker1" Width="140" IsEnabled="{Binding IsChecked, ElementName=cbDateOfJoining}" /> </WrapPanel> <WrapPanel> <CheckBox Height="16" Name="cbDateOfLeaving" Width="120">Date of leaving</CheckBox> <wpf:DatePicker Height="25" Name="datePicker2" Width="140" IsEnabled="{Binding IsChecked, ElementName=cbDateOfLeaving}" Style="{DynamicResource DatePickerStyle1}" /> </WrapPanel> </StackPanel> </Grid> </Window> 

Hope that helps!

And I hope that it is fixed correctly in the next version of WPFtoolkit. Users have been complaining about this issue since 2009 ...

+4
source share

Another way to fake the gray / off style is to abuse the IsHitTestVisible property.

 <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wpf="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit" x:Class="CalendarTest.Window1" x:Name="Window" Title="MainWindow" Width="640" Height="480"> <StackPanel> <CheckBox x:Name="IsCalendarEnabled" Content="Is Calendar Enabled" /> <wpf:DatePicker IsHitTestVisible="{Binding ElementName=IsCalendarEnabled, Path=IsChecked}"> <wpf:DatePicker.Style> <Style TargetType="{x:Type wpf:DatePicker}"> <Style.Triggers> <Trigger Property="IsHitTestVisible" Value="False"> <Setter Property="Foreground" Value="#FFB9B9B9" /> </Trigger> </Style.Triggers> </Style> </wpf:DatePicker.Style> </wpf:DatePicker> </StackPanel> </Window> 

Another temporary workaround to fixing the IsEnabled error. issue 7904 / issue 9641

+1
source share

Has anyone ever received an answer to this? I have the same issue with the February 2009 release.

Try it. 1 / Use the tab control and put the datupick on the first tab and one on the second. 2 / Add a button, and in the on click event, set the IsEnabled property of both date picker controls to the opposite value (therefore, if it is enabled, disable it and vice versa).

On the first tab, the datepicker will be fine. In the second case, it will be disabled, but you can still open the calendar and change the date.

0
source share

this problem was resolved in the new WPFToolkit

-one
source share

All Articles