tags:

views:

35

answers:

1

The text is grayed out when the DatePicker is disabled and I want the content to be easier to read.

What I did on some TextBoxes was:

<Style TargetType="TextBox">
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Foreground" Value="Black" />
            </Trigger>
        </Style.Triggers>
</Style>

It did make the text easier to read.

I do manage to change the Foreground colour on the DataPicker but it does not do the trick. The text was still grayed out.

Seems like there is another property I need to set to make the content of the disabled DatePicker easier to read.

So, how do I make the content of my disabled DatePicker easier to read?

A: 

Can you extend DatePicker by adding bool DependencyProperty called Editable.
I found a working example at the following link, note that I run this code in .NET 4.

Here is the DatePicker Control:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;

namespace DatePickerStyle
{
 public class ExtendedDatePicker : DatePicker
 {
    public static readonly DependencyProperty EditableProperty = DependencyProperty.Register("Editable", typeof(bool),
        typeof(ExtendedDatePicker), new PropertyMetadata(true));
    public bool Editable
    {
       get { return (bool)GetValue(EditableProperty); }
       set { SetValue(EditableProperty, value); }
    }

    public override void OnApplyTemplate()
    {
       base.OnApplyTemplate();
       var textBox = GetTemplateChild("PART_TextBox") as DatePickerTextBox;
       var binding = new Binding { Source = this, Path = new PropertyPath(ExtendedDatePicker.EditableProperty) };
       textBox.SetBinding(UIElement.FocusableProperty, binding);
    }
  }
}

Here is the XAML:

<Window x:Class="DatePickerStyle.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:DatePickerStyle="clr-namespace:DatePickerStyle" 
    Title="MainWindow" Height="350" Width="525">
  <Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <DatePicker IsEnabled="True" Grid.Row="0" SelectedDate="2002/12/31"/>
    <DatePicker IsEnabled="False" Grid.Row="1" SelectedDate="2002/12/31"/>
    <DatePickerStyle:ExtendedDatePicker Editable="True" Grid.Row="2" SelectedDate="2002/12/31"/>
    <DatePickerStyle:ExtendedDatePicker Editable="False" Grid.Row="3" SelectedDate="2002/12/31"/>
  </Grid>
</Window>
Zamboni