Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Silverlight 3
Controls
 DatePicker Styles and Templates

  Switch on low bandwidth view
Silverlight
DatePicker Styles and Templates

This topic describes the styles and templates for the DatePicker control. You can modify the default ControlTemplate to give the control a unique appearance. For more information, see Customizing the Appearance of an Existing Control by Using a ControlTemplate.

The following table lists the named parts for the DatePicker control.

DatePicker Part

Type

Description

Root

Grid

The root of the control.

Button

Button

The button that opens and closes the Calendar.

TextBox

DatePickerTextBox

The text box that allows you to input a date.

Popup

Popup

The popup for the DatePicker control.

The following table lists the visual states for the DatePicker control.

DatePicker VisualState Name

VisualStateGroup Name

Description

Normal

CommonStates

The default state.

Disabled

CommonStates

The state that is triggered when the DatePicker is disabled.

Valid

ValidationStates

The DatePicker control is valid.

InvalidFocused

ValidationStates

The DatePicker control is not valid and has focus.

InvalidUnfocused

ValidationStates

The DatePicker control is not valid and does not have focus.

The following table lists the Style properties of the DatePicker control. You must set the TargetType property when you create a Style.

Property Name

Target Type

Description

CalendarStyle

Calendar

The style applied to the calendar that displays the date.

The following example shows how to add a custom style as a StaticResource. For more information, see Customizing the Appearance of an Existing Control by Using a ControlTemplate. In this example, the StaticResource refers to an x:Key attribute value that you must add to the Style element when you modify it for your project.

<DatePicker x:Name="datePicker1"
    Style="{StaticResource newDatePickerStyle}" />

The following shows the XML namespace definition that you have to specify when you work with styles and templates.

<!-- XML Name Space definitions. -->
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"

The following XAML shows the default styles and templates for the DatePicker control.

<Style TargetType="controls:DatePicker">
    <Setter Property="IsTabStop" Value="False" />
    <Setter Property="Background" Value="#FFFFFFFF" />
    <Setter Property="Padding" Value="2" />
    <Setter Property="SelectionBackground" Value="#FF444444" />
    <Setter Property="BorderBrush">
        <Setter.Value>
            <LinearGradientBrush EndPoint=".5,0" StartPoint=".5,1">
                <GradientStop Color="#FF617584" Offset="0" />
                <GradientStop Color="#FF718597" Offset="0.375" />
                <GradientStop Color="#FF8399A9" Offset="0.375" />
                <GradientStop Color="#FFA3AEB9" Offset="1" />
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="controls:DatePicker">
                <Grid x:Name="Root">
                    <Grid.Resources>
                        <!-- Main DatePicker Brushes -->
                        <SolidColorBrush x:Key="DisabledBrush" Color="#8CFFFFFF" />

                        <!-- Button Template -->
                        <ControlTemplate x:Key="DropDownButtonTemplate" TargetType="Button">
                            <Grid>
                                <vsm:VisualStateManager.VisualStateGroups>
                                    <vsm:VisualStateGroup x:Name="CommonStates">
                                        <vsm:VisualStateGroup.Transitions>
                                            <vsm:VisualTransition GeneratedDuration="0" />
                                            <vsm:VisualTransition To="MouseOver" GeneratedDuration="0:0:0.1" />
                                            <vsm:VisualTransition To="Pressed" GeneratedDuration="0:0:0.1" />
                                        </vsm:VisualStateGroup.Transitions>
                                        <vsm:VisualState x:Name="Normal" />
                                        <vsm:VisualState x:Name="MouseOver">
                                            <Storyboard>
                                                <ColorAnimation Duration="0" Storyboard.TargetName="Background" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" To="#FF448DCA" />
                                                <ColorAnimationUsingKeyFrames BeginTime="0" Duration="00:00:00.001" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[3].(GradientStop.Color)">
                                                    <SplineColorKeyFrame KeyTime="0" Value="#7FFFFFFF" />
                                                </ColorAnimationUsingKeyFrames>
                                                <ColorAnimationUsingKeyFrames BeginTime="0" Duration="00:00:00.001" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[2].(GradientStop.Color)">
                                                    <SplineColorKeyFrame KeyTime="0" Value="#CCFFFFFF" />
                                                </ColorAnimationUsingKeyFrames>
                                                <ColorAnimationUsingKeyFrames BeginTime="0" Duration="00:00:00.001" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
                                                    <SplineColorKeyFrame KeyTime="0" Value="#F2FFFFFF" />
                                                </ColorAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </vsm:VisualState>
                                        <vsm:VisualState x:Name="Pressed">
                                            <Storyboard>
                                                <ColorAnimationUsingKeyFrames BeginTime="0" Duration="00:00:00.001" Storyboard.TargetName="Background" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)">
                                                    <SplineColorKeyFrame KeyTime="0" Value="#FF448DCA" />
                                                </ColorAnimationUsingKeyFrames>
                                                <DoubleAnimationUsingKeyFrames BeginTime="0" Duration="00:00:00.001" Storyboard.TargetName="Highlight" Storyboard.TargetProperty="(UIElement.Opacity)">
                                                    <SplineDoubleKeyFrame KeyTime="0" Value="1" />
                                                </DoubleAnimationUsingKeyFrames>
                                                <ColorAnimationUsingKeyFrames BeginTime="0" Duration="00:00:00.001" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
                                                    <SplineColorKeyFrame KeyTime="0" Value="#EAFFFFFF" />
                                                </ColorAnimationUsingKeyFrames>
                                                <ColorAnimationUsingKeyFrames BeginTime="0" Duration="00:00:00.001" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[2].(GradientStop.Color)">
                                                    <SplineColorKeyFrame KeyTime="0" Value="#C6FFFFFF" />
                                                </ColorAnimationUsingKeyFrames>
                                                <ColorAnimationUsingKeyFrames BeginTime="0" Duration="00:00:00.001" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[3].(GradientStop.Color)">
                                                    <SplineColorKeyFrame KeyTime="0" Value="#6BFFFFFF" />
                                                </ColorAnimationUsingKeyFrames>
                                                <ColorAnimationUsingKeyFrames BeginTime="0" Duration="00:00:00.001" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
                                                    <SplineColorKeyFrame KeyTime="0" Value="#F4FFFFFF" />
                                                </ColorAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </vsm:VisualState>
                                        <vsm:VisualState x:Name="Disabled">
                                            <Storyboard>
                                                <DoubleAnimationUsingKeyFrames BeginTime="0" Duration="00:00:00.001" Storyboard.TargetName="DisabledVisual" Storyboard.TargetProperty="(UIElement.Opacity)">
                                                    <SplineDoubleKeyFrame KeyTime="0" Value="1" />
                                                </DoubleAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </vsm:VisualState>
                                    </vsm:VisualStateGroup>
                                </vsm:VisualStateManager.VisualStateGroups>

                                <!--Start UI-->
                                <Grid Height="18" Width="19" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0" Background="#11FFFFFF">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="20*" />
                                        <ColumnDefinition Width="20*" />
                                        <ColumnDefinition Width="20*" />
                                        <ColumnDefinition Width="20*" />
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="23*" />
                                        <RowDefinition Height="19*" />
                                        <RowDefinition Height="19*" />
                                        <RowDefinition Height="19*" />
                                    </Grid.RowDefinitions>
                                    <Border Margin="-1" Grid.ColumnSpan="4" Grid.Row="0" Grid.RowSpan="4" BorderThickness="1" BorderBrush="#FF6DBDD1" Opacity="0" CornerRadius="0,0,1,1" x:Name="Highlight" />
                                    <Border x:Name="Background" Margin="0,-1,0,0" Grid.ColumnSpan="4" Grid.Row="1" Grid.RowSpan="3" BorderThickness="1" BorderBrush="#FFFFFFFF" Opacity="1" CornerRadius=".5" Background="#FF1F3B53" />
                                    <Border x:Name="BackgroundGradient" Margin="0,-1,0,0" Grid.ColumnSpan="4" Grid.Row="1" Grid.RowSpan="3" BorderThickness="1" BorderBrush="#BF000000" Opacity="1" CornerRadius=".5">
                                        <Border.Background>
                                            <LinearGradientBrush StartPoint=".7,0" EndPoint=".7,1">
                                                <GradientStop Color="#FFFFFFFF" Offset="0" />
                                                <GradientStop Color="#F9FFFFFF" Offset="0.375" />
                                                <GradientStop Color="#E5FFFFFF" Offset="0.625" />
                                                <GradientStop Color="#C6FFFFFF" Offset="1" />
                                            </LinearGradientBrush>
                                        </Border.Background>
                                    </Border>
                                    <Rectangle Grid.ColumnSpan="4" Grid.RowSpan="1" StrokeThickness="1">
                                        <Rectangle.Stroke>
                                            <LinearGradientBrush EndPoint="0.48,-1" StartPoint="0.48,1.25">
                                                <GradientStop Color="#FF494949" />
                                                <GradientStop Color="#FF9F9F9F" Offset="1" />
                                            </LinearGradientBrush>
                                        </Rectangle.Stroke>
                                        <Rectangle.Fill>
                                            <LinearGradientBrush EndPoint="0.3,-1.1" StartPoint="0.46,1.6">
                                                <GradientStop Color="#FF4084BD" />
                                                <GradientStop Color="#FFAFCFEA" Offset="1" />
                                            </LinearGradientBrush>
                                        </Rectangle.Fill>
                                    </Rectangle>
                                    <Path HorizontalAlignment="Center" Margin="4,3,4,3" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" Grid.Column="0" Grid.Row="1" Fill="#FF2F2F2F" Stretch="Fill" Data="M11.426758,8.4305077 L11.749023,8.4305077 L11.749023,16.331387 L10.674805,16.331387 L10.674805,10.299648 L9.0742188,11.298672 L9.0742188,10.294277 C9.4788408,10.090176 9.9094238,9.8090878 10.365967,9.4510155 C10.82251,9.0929432 11.176106,8.7527733 11.426758,8.4305077 z M14.65086,8.4305077 L18.566387,8.4305077 L18.566387,9.3435936 L15.671368,9.3435936 L15.671368,11.255703 C15.936341,11.058764 16.27293,10.960293 16.681133,10.960293 C17.411602,10.960293 17.969301,11.178717 18.354229,11.615566 C18.739157,12.052416 18.931622,12.673672 18.931622,13.479336 C18.931622,15.452317 18.052553,16.438808 16.294415,16.438808 C15.560365,16.438808 14.951641,16.234707 14.468243,15.826504 L14.881817,14.929531 C15.368796,15.326992 15.837872,15.525723 16.289043,15.525723 C17.298809,15.525723 17.803692,14.895514 17.803692,13.635098 C17.803692,12.460618 17.305971,11.873379 16.310528,11.873379 C15.83071,11.873379 15.399232,12.079271 15.016094,12.491055 L14.65086,12.238613 z" Grid.ColumnSpan="4" Grid.RowSpan="3" />
                                    <Ellipse HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFFFFFFF" StrokeThickness="0" Grid.ColumnSpan="4" Width="3" Height="3" />
                                    <Border Grid.ColumnSpan="4" Grid.Row="0" Grid.RowSpan="4" BorderThickness="1" BorderBrush="#B2FFFFFF" Opacity="0" CornerRadius="0,0,.5,.5" x:Name="DisabledVisual" />
                                </Grid>
                                <!--End UI-->
                            </Grid>
                        </ControlTemplate>
                    </Grid.Resources>
                    <vsm:VisualStateManager.VisualStateGroups>
                        <vsm:VisualStateGroup x:Name="CommonStates">
                            <vsm:VisualState x:Name="Normal" />
                            <vsm:VisualState x:Name="Disabled">
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="DisabledVisual" Storyboard.TargetProperty="Opacity" To="1" Duration="0" />
                                </Storyboard>
                            </vsm:VisualState>
                        </vsm:VisualStateGroup>
                    </vsm:VisualStateManager.VisualStateGroups>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <controlsPrimitives:DatePickerTextBox x:Name="TextBox" SelectionBackground="{TemplateBinding SelectionBackground}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" Grid.Column="0" />
                    <Button x:Name="Button" Template="{StaticResource DropDownButtonTemplate}" Grid.Column="1" Width="20" Foreground="{TemplateBinding Foreground}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Margin="2,0,2,0" />
                    <Grid x:Name="DisabledVisual" Opacity="0" IsHitTestVisible="False" Grid.ColumnSpan="2">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <Rectangle RadiusX="1" RadiusY="1" Fill="#8CFFFFFF" />
                        <Rectangle RadiusX="1" RadiusY="1" Fill="#8CFFFFFF" Grid.Column="1" Height="18" Width="19" Margin="2,0,2,0" />
                    </Grid>
                    <Popup x:Name="Popup" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Missing example references?      MSearles   |   Edit   |   Show History
Hi,

Possibly not the best place to put this, but pasting the example into a ResourceDictionary throws up errors

The attachable property 'VisualStateGroups' was not found in type 'VisualStateManager'.

and

The type 'primitives:DatePickerTextBox' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.

Could the relevant xmlns's be included in the sample?

Thanks,
Matt


XMLNS for the Sample      Wolf Schmidt - MSFT   |   Edit   |   Show History
Hi: sorry about missing the xmlns. What happens when we compose the example code is we pull that code out of larger projects. The xmlns is almost always on the root and yet we don't want to show you all the XAML including the root for some of the larger files. We have a way to do this but it's tricky and sometimes we ummm forget.

xmlns:local="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"

xmlns:primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls"

xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
About the vsm xmlns. The prefix exists because some of the design tools had a glitch about picking up the readonly attached property VisualStateManager.VisualStateGroups without special casing it into a custom namespace. But I'm not positive that the requirement to use vsm for mapping VisualStateManager still exists with the XAML design tools that are released.

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker