Jerry Nixon @Work: Simply change a WPF DatePicker's disabled Style

Jerry Nixon on Windows

Tuesday, July 19, 2011

Simply change a WPF DatePicker's disabled Style

The reality is that the disabled style of a DatePicker in WPF is difficult to read. You COULD restyle the whole freaking thing. Or you could do something simple, like just re-templating the control based on the IsEnabled property.

Like this:

<Window x:Class="WpfApplication1.MainWindow"
        Title="MainWindow" Height="650" Width="550">
        <sys:DateTime x:Key="MyValue">1/1/2000</sys:DateTime>
        <Style TargetType="{x:Type DatePicker}">
              <Trigger Property="IsEnabled" Value="false">
                 <Setter Property="Template">
                             <Border BorderBrush="Black" BorderThickness="1">
                                 <TextBlock Text="{Binding Path=SelectedDate,
                                 RelativeSource={RelativeSource TemplatedParent}}"
                                 VerticalAlignment="Center" HorizontalAlignment="Left"
                                 Padding="4,0,0,0" />
    <DatePicker IsEnabled="False" SelectedDate="{StaticResource MyValue}" 
                Width="100" Height="25" />