Click here to Skip to main content
15,883,896 members
Articles / Desktop Programming / WPF

A couple of tricks when using the standard WPF .NET 4.0 DatePicker control

Rate me:
Please Sign up or sign in to vote.
4.88/5 (48 votes)
9 Mar 2012CPOL6 min read 210.3K   3K   62  
Show hows to alter the DatePicker to use keyboard up/down keys for cleverer date selection.
<Window x:Class="DatePickerTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DatePickerTest"
        Title="MainWindow"
        Height="350"
        Width="525">

    <Window.Resources>


        <!-- CalendarDayButton Style -->
        <Style x:Key="CalendarDayButtonStyleEx"
                TargetType="{x:Type CalendarDayButton}">
            <Setter Property="MinWidth"
                    Value="5" />
            <Setter Property="MinHeight"
                    Value="5" />
            <Setter Property="FontSize"
                    Value="10" />
            <Setter Property="HorizontalContentAlignment"
                    Value="Center" />
            <Setter Property="VerticalContentAlignment"
                    Value="Center" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type CalendarDayButton}">


                        <Grid x:Name="CalendarDayButtonGrid">


                            <Grid.ToolTip>
                                <MultiBinding Converter="{local:HighlightDateConverter}">
                                    <MultiBinding.Bindings>
                                        <Binding />
                                        <Binding RelativeSource="{RelativeSource FindAncestor, 
                                            AncestorType={x:Type local:DatePickerEx}}" />
                                    </MultiBinding.Bindings>
                                </MultiBinding>
                            </Grid.ToolTip>

                            
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualStateGroup.Transitions>
                                        <VisualTransition GeneratedDuration="0:0:0.1" />
                                    </VisualStateGroup.Transitions>
                                    <VisualState x:Name="Normal" />
                                    <VisualState x:Name="MouseOver">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0"
                                                To="0.5"
                                                Storyboard.TargetProperty="Opacity"
                                                Storyboard.TargetName="HighlightBackground" />
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Pressed">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0"
                                                To="0.5"
                                                Storyboard.TargetProperty="Opacity"
                                                Storyboard.TargetName="HighlightBackground" />
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0"
                                                To="0"
                                                Storyboard.TargetProperty="Opacity"
                                                Storyboard.TargetName="HighlightBackground" />
                                            <DoubleAnimation Duration="0"
                                                To=".35"
                                                Storyboard.TargetProperty="Opacity"
                                                Storyboard.TargetName="NormalText" />
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="SelectionStates">
                                    <VisualStateGroup.Transitions>
                                        <VisualTransition GeneratedDuration="0" />
                                    </VisualStateGroup.Transitions>
                                    <VisualState x:Name="Unselected" />
                                    <VisualState x:Name="Selected">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0"
                                                To=".75"
                                                Storyboard.TargetProperty="Opacity"
                                                Storyboard.TargetName="SelectedBackground" />
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="CalendarButtonFocusStates">
                                    <VisualStateGroup.Transitions>
                                        <VisualTransition GeneratedDuration="0" />
                                    </VisualStateGroup.Transitions>
                                    <VisualState x:Name="CalendarButtonFocused">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Duration="0"
                                                    Storyboard.TargetProperty="Visibility"
                                                    Storyboard.TargetName="DayButtonFocusVisual">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <Visibility>Visible</Visibility>
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="CalendarButtonUnfocused">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Duration="0"
                                                Storyboard.TargetProperty="Visibility"
                                                Storyboard.TargetName="DayButtonFocusVisual">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <Visibility>Collapsed</Visibility>
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="ActiveStates">
                                    <VisualStateGroup.Transitions>
                                        <VisualTransition GeneratedDuration="0" />
                                    </VisualStateGroup.Transitions>
                                    <VisualState x:Name="Active" />
                                    <VisualState x:Name="Inactive">
                                        <Storyboard>
                                            <ColorAnimation Duration="0"
                                                To="#FF777777"
                                                Storyboard.TargetProperty=
                                                    "(TextElement.Foreground).(SolidColorBrush.Color)"
                                                Storyboard.TargetName="NormalText" />
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="DayStates">
                                    <VisualStateGroup.Transitions>
                                        <VisualTransition GeneratedDuration="0" />
                                    </VisualStateGroup.Transitions>
                                    <VisualState x:Name="RegularDay" />
                                    <VisualState x:Name="Today">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0"
                                                To="1"
                                                Storyboard.TargetProperty="Opacity"
                                                Storyboard.TargetName="TodayBackground" />
                                            <ColorAnimation Duration="0"
                                                To="#FFFFFFFF"
                                                Storyboard.TargetProperty=
                                                    "(TextElement.Foreground).(SolidColorBrush.Color)"
                                                Storyboard.TargetName="NormalText" />
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="BlackoutDayStates">
                                    <VisualStateGroup.Transitions>
                                        <VisualTransition GeneratedDuration="0" />
                                    </VisualStateGroup.Transitions>
                                    <VisualState x:Name="NormalDay" />
                                    <VisualState x:Name="BlackoutDay">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0"
                                                                To=".2"
                                                                Storyboard.TargetProperty="Opacity"
                                                                Storyboard.TargetName="Blackout" />
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Rectangle x:Name="TodayBackground"
                                        Fill="#FFAAAAAA"
                                        Opacity="0"
                                        RadiusY="1"
                                        RadiusX="1" />

                            <Rectangle x:Name="AccentBackground"
                                        RadiusX="1"
                                        RadiusY="1"
                                        IsHitTestVisible="False"
                                        Fill="{Binding RelativeSource={RelativeSource AncestorType=Calendar}, 
                                            Path=DateHighlightBrush}" />

                            <Rectangle x:Name="SelectedBackground"
                                        Fill="#FFBADDE9"
                                        Opacity="0"
                                        RadiusY="1"
                                        RadiusX="1" />
                            <Border BorderBrush="{TemplateBinding BorderBrush}"
                                    BorderThickness="{TemplateBinding BorderThickness}"
                                    Background="{TemplateBinding Background}" />
                            <Rectangle x:Name="HighlightBackground"
                                        Fill="#FFBADDE9"
                                        Opacity="0"
                                        RadiusY="1"
                                        RadiusX="1" />
                            <ContentPresenter x:Name="NormalText"
                                                TextElement.Foreground="#FF333333"
                                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                Margin="5,1,5,1"
                                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                            <Path x:Name="Blackout"
                                    Data="M8.1772461,11.029181 L10.433105,11.029181 L11.700684,12.801641 
                                    L12.973633,11.029181 L15.191895,11.029181 L12.844727,13.999395 
                                    L15.21875,17.060919 L12.962891,17.060919 L11.673828,15.256231 
                                    L10.352539,17.060919 L8.1396484,17.060919 L10.519043,14.042364 z"
                                    Fill="#FF000000"
                                    HorizontalAlignment="Stretch"
                                    Margin="3"
                                    Opacity="0"
                                    RenderTransformOrigin="0.5,0.5"
                                    Stretch="Fill"
                                    VerticalAlignment="Stretch" />
                            <Rectangle x:Name="DayButtonFocusVisual"
                                        IsHitTestVisible="false"
                                        RadiusY="1"
                                        RadiusX="1"
                                        Stroke="#FF45D6FA"
                                        Visibility="Collapsed" />
                        </Grid>

                        <ControlTemplate.Triggers>

                            <!-- No ToolTip highlighting if IValueConverter returned null -->
                            <DataTrigger Value="{x:Null}">
                                <DataTrigger.Binding>
                                    <MultiBinding Converter="{local:HighlightDateConverter}">
                                        <MultiBinding.Bindings>
                                            <Binding />
                                            <Binding RelativeSource=
                                                        "{RelativeSource FindAncestor, AncestorType={x:Type local:DatePickerEx}}" />
                                        </MultiBinding.Bindings>
                                    </MultiBinding>

                                </DataTrigger.Binding>
                                <Setter TargetName="AccentBackground"
                                        Property="Visibility"
                                        Value="Hidden" />
                            </DataTrigger>

                        </ControlTemplate.Triggers>

                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>




        <Style x:Key="calendarWithGotToTodayStyle"
                TargetType="{x:Type Calendar}">
            <Setter Property="Foreground"
                    Value="#FF333333" />
            <Setter Property="Background"
                    Value="White" />
            <Setter Property="BorderBrush"
                    Value="DarkGray" />
            <Setter Property="BorderThickness"
                    Value="1" />
            <Setter Property="CalendarDayButtonStyle"
                    Value="{StaticResource CalendarDayButtonStyleEx}" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Calendar}">
                        <Border BorderThickness="{TemplateBinding BorderThickness}"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}">
                            <StackPanel Orientation="Vertical">
                                <StackPanel x:Name="PART_Root"
                                            HorizontalAlignment="Center">
                                    <CalendarItem x:Name="PART_CalendarItem"
                                                    BorderBrush="{TemplateBinding BorderBrush}"
                                                    BorderThickness="{TemplateBinding BorderThickness}"
                                                    Background="{TemplateBinding Background}"
                                                    Style="{TemplateBinding CalendarItemStyle}" />
                                    <Button  x:Name="PART_GoToTodayButton"
                                                HorizontalAlignment="Center"
                                                VerticalAlignment="Center"
                                                Margin="10"
                                                Content="Go To Today" />
                                </StackPanel>
                            </StackPanel>
                        </Border>

                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>






    </Window.Resources>



    <StackPanel>
        <Label Content="Standard Microsoft DatePicker" />
        <DatePicker Margin="10"
                    x:Name="dp"
                    Background="LightGray" />
        <Label Content="DatePicker which allow up/down dates" />
        <local:DatePickerEx Margin="10"
                                   x:Name="mdp"
                                   AlternativeCalendarStyle="{StaticResource calendarWithGotToTodayStyle}"
                                   Background="Green" />
        <Button Content="What's the date"
                Margin="10,30,10,10"
                Click="WhatsTheData_Click" />
    </StackPanel>
</Window>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions