views:

472

answers:

5

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;
    }
A: 

this issue has been solved in the new WPFToolkit

Kishore Kumar
In the Feb Toolkit?
Jonathan
we are using jun 2009 release.
Kishore Kumar
Well this is in the latests toolkit which dates Feb 2010
Jonathan
A: 

Did anyone ever get an answer to this? I am having the same issue with the Feb 2009 release.

Try this.
1/ Use a tab control and put a datepicker 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 datepicker controls to the inverse value (so if it was Enabled, make it disabled and vice versa).

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

Glenn Smith
How about posting a user control that abstracts it for us?
Jonathan
A: 

This is not fixed in the February 2010 release WPF Toolkit. I have that release and it functions exactly as he described. The control will not enable at runtime when disabled in XAML.

Wade
+2  A: 

Do you want the good news, or the bad news ?

The good news is that Microsoft says that this issue has been fixed in the Feb 2010 release of the WPFToolkit. And it has.

The bad news is that, although setting a DatePicker's IsEnabled value to "True" will enable the DatePicker, so you can now click on the Calendar button to pick a date, it will still look disabled.

"Bug" ? Did I say the word "bug" ?

Of course not.

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

Below is some simple xaml code to demonstrate.

It displays two rows, each containing a CheckBox and a DatePicker. When you click on a CheckBox in a row, it should enable the DatePicker in that row.

This shows the difference between a DatePicker without a Style (in the first row), and a DatePicker with a Style (in the second row).

alt text

Both DatePickers do get enabled/disabled correctly, but the one in the first row never looks as though it is. The DatePicker in the second row uses a Style to show the user when it's disabled.

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

<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 the helps !

And I hope that it's properly fixed in the next WPFtoolkit release. Users have been complaining about this issue since 2009...

Mike Gledhill
A: 

I've downloaded wpf toolkit and no solution seems to work for me, whatever tricks I try to use to make the control disabled, my app crashes.

Greg