Customizing Calendar Control In Silverlight 3 Using Blend 3 – Part I


Introduction

In this article we will customize the Calendar control, this is going to be a long documents so I decided customizing some parts at a time.

Creating Silverlight 3 Application

Open up Blend 3 and create a Silverlight 3 Application. Name it as MyCalendarInSL3P1.

Go ahead and draw a Calendar into your application.

To customize it, right click on it and you will see a menu item called Edit Template.

 

Now there is another option called Edit Additional Templates. If you select it you will find three Styles to be edited. Such as: Calendar Button Style, Calendar Day Button Style and Calendar Item Style.

We will proceed with the Edit Calendar Item Style in this article. Select Edit Calendar Item Style and Edit a copy.

As soon as you select Edit a Copy, a dialog pops up saying the Style Resource. Name it as MyCalendarItemStyle.

After you press OK, you are in Edit Template mode of the Calendar Item. See the Object and Timeline Pane to see the hierarchy of controls.

Suppose you don’t want the Next and Previous Button.

Delete the NextButton and the PreviousButton. Your calendar will look like below.

Now we will change the Day Button Style to look something good.

Go ahead and exit from this edit template mode and again right click on Calendar and select Edit Calendar Day Button Style and select Edit a copy.

Name the Style as MyCalendarDayButtonStyle when the dialog pops up.

Now if you see in your Objects and Timeline Pane you will find the following hierarchy of controls.

First, we will change the Focus Visual. By default the Stroke is LightBlue. We will change it to Red. So that when you select the day the border of your day will be Red. Similar to the following:

Now we will change the Background. Notice that when you select the Background Rectangle you are not allowed to change the Brushes. You can override it by selecting the small yellow mark and a new set of options will pop up.

Select Reset and now you can change any color you want. I have made it to light red. Looks something similar to the following:

Now if you want to change the Selected Background Color then go ahead follow the above steps and change it. I have changed it to yellow.

Now we change Today’s Background. By default it’s gray in color. I have changed it to Violate. This time you are allowed to change it. So go ahead and change it.

That’s it you are customized a lot for your own style. We will see some more customizations in the next article.

Enjoy Coding.

Here is the XAML code behind for your reference.

<UserControl.Resources>

<Style x:Key=”MyCalendarItemStyle” TargetType=”System_Windows_Controls_Primitives:CalendarItem”>

    <Setter Property=”Template”>

        <Setter.Value>

            <ControlTemplate TargetType=”System_Windows_Controls_Primitives:CalendarItem”>

            <Grid>

            <Grid.Resources>

            <SolidColorBrush x:Key=”DisabledBrush” Color=”#8CFFFFFF”/>

            </Grid.Resources>

        <VisualStateManager.CustomVisualStateManager>

        <ic:ExtendedVisualStateManager/>

        </VisualStateManager.CustomVisualStateManager>

        <VisualStateManager.VisualStateGroups>

        <VisualStateGroup x:Name=”CommonStates”>

                <VisualState x:Name=”Normal”/>

                <VisualState x:Name=”Disabled”>

        <Storyboard>

<DoubleAnimation Duration=”0″ Storyboard.TargetName=”DisabledVisual” Storyboard.TargetProperty=”Opacity” To=”1″/>

</Storyboard>

</VisualState>

</VisualStateGroup>

</VisualStateManager.VisualStateGroups>

<Border Margin=”0,2,0,2″ Background=”{TemplateBinding Background}” BorderBrush=”{TemplateBinding BorderBrush}” BorderThickness=”{TemplateBinding BorderThickness}” CornerRadius=”1″>

<Border BorderBrush=”#FFFFFFFF” BorderThickness=”2″ CornerRadius=”1″>

<Grid>

<Grid.Resources>

<ControlTemplate x:Key=”HeaderButtonTemplate” TargetType=”Button”>

<Grid Cursor=”Hand”>

<VisualStateManager.VisualStateGroups>

<VisualStateGroup x:Name=”CommonStates”>

<VisualState x:Name=”Normal”/>

<VisualState x:Name=”MouseOver”>

<Storyboard>

<ColorAnimation Duration=”0″ Storyboard.TargetName=”ContentBrush” Storyboard.TargetProperty=”Color” To=”#FF73A9D8″/>

</Storyboard>

</VisualState>

<VisualState x:Name=”Disabled”>

<Storyboard>

<DoubleAnimation Duration=”0″ Storyboard.TargetName=”Content” Storyboard.TargetProperty=”Opacity” To=”.5″/>

</Storyboard>

</VisualState>

    </VisualStateGroup>

    </VisualStateManager.VisualStateGroups>

    <ContentControl x:Name=”Content” HorizontalAlignment=”{TemplateBinding HorizontalContentAlignment}” Margin=”1,5,1,9″ VerticalAlignment=”{TemplateBinding VerticalContentAlignment}” IsTabStop=”False” Content=”{TemplateBinding Content}” ContentTemplate=”{TemplateBinding ContentTemplate}”>

    <ContentControl.Foreground>

        <SolidColorBrush x:Name=”ContentBrush” Color=”#FF333333″/>

    </ContentControl.Foreground>

    </ContentControl>

</Grid>

</ControlTemplate>

<DataTemplate x:Name=”DayTitleTemplate”>

<TextBlock HorizontalAlignment=”Center” Margin=”0,4,0,4″ VerticalAlignment=”Center” FontSize=”9.5″ FontWeight=”Bold” Text=”{Binding}”/>

</DataTemplate>

<ControlTemplate x:Key=”PreviousButtonTemplate” TargetType=”Button”>

<Grid Cursor=”Hand”>

<VisualStateManager.VisualStateGroups>

<VisualStateGroup x:Name=”CommonStates”>

        <VisualState x:Name=”Normal”/>

        <VisualState x:Name=”MouseOver”>

            <Storyboard>

                <ColorAnimation Duration=”0″ Storyboard.TargetName=”IconBrush” Storyboard.TargetProperty=”Color” To=”#FF73A9D8″/>

            </Storyboard>

        </VisualState>

        <VisualState x:Name=”Disabled”>

            <Storyboard>

<DoubleAnimation Duration=”0″ Storyboard.TargetName=”IconBrush” Storyboard.TargetProperty=”Opacity” To=”.5″/>

            </Storyboard>

        </VisualState>

    </VisualStateGroup>

    </VisualStateManager.VisualStateGroups>

    <Rectangle Fill=”#11E5EBF1″ Stretch=”Fill” Opacity=”1″/>

        <Grid>

    <Path Stretch=”Fill” Height=”10″ HorizontalAlignment=”Left” Margin=”14,-6,0,0″ VerticalAlignment=”Center” Width=”6″ Data=”M288.75,232.25 L288.75,240.625 L283,236.625 z”>

    <Path.Fill>

    <SolidColorBrush x:Name=”IconBrush” Color=”#FF333333″/>

    </Path.Fill>

    </Path>

    </Grid>

    </Grid>

    </ControlTemplate>

    <ControlTemplate x:Key=”NextButtonTemplate” TargetType=”Button”>

    <Grid Cursor=”Hand”>

    <VisualStateManager.VisualStateGroups>

    <VisualStateGroup x:Name=”CommonStates”>

        <VisualState x:Name=”Normal”/>

        <VisualState x:Name=”MouseOver”>

            <Storyboard>

    <ColorAnimation Duration=”0″ Storyboard.TargetName=”IconBrush” Storyboard.TargetProperty=”Color” To=”#FF73A9D8″/>

            </Storyboard>

        </VisualState>

        <VisualState x:Name=”Disabled”>

            <Storyboard>

<DoubleAnimation Duration=”0″ Storyboard.TargetName=”IconBrush” Storyboard.TargetProperty=”Opacity” To=”.5″/>

            </Storyboard>

        </VisualState>

    </VisualStateGroup>

    </VisualStateManager.VisualStateGroups>

    <Rectangle Fill=”#11E5EBF1″ Stretch=”Fill” Opacity=”1″/>

    <Grid>

    <Path Stretch=”Fill” Height=”10″ HorizontalAlignment=”Right” Margin=”0,-6,14,0″ VerticalAlignment=”Center” Width=”6″ Data=”M282.875,231.875 L282.875,240.375 L288.625,236 z”>

        <Path.Fill>

            <SolidColorBrush x:Name=”IconBrush” Color=”#FF333333″/>

        </Path.Fill>

    </Path>

    </Grid>

</Grid>

    </ControlTemplate>

</Grid.Resources>

<Grid.RowDefinitions>

<RowDefinition Height=”Auto”/>

<RowDefinition Height=”*”/>

</Grid.RowDefinitions>

<Grid.ColumnDefinitions>

    <ColumnDefinition Width=”Auto”/>

    <ColumnDefinition Width=”Auto”/>

    <ColumnDefinition Width=”Auto”/>

    </Grid.ColumnDefinitions>

<Button x:Name=”HeaderButton” HorizontalAlignment=”Center” VerticalAlignment=”Center” FontSize=”10.5″ FontWeight=”Bold” Template=”{StaticResource HeaderButtonTemplate}” Grid.Column=”1″/>

<Grid x:Name=”MonthView” Margin=”6,-1,6,6″ Visibility=”Collapsed” Grid.ColumnSpan=”3″ Grid.Row=”1″>

    <Grid.RowDefinitions>

    <RowDefinition Height=”Auto”/>

    <RowDefinition Height=”Auto”/>

    <RowDefinition Height=”Auto”/>

    <RowDefinition Height=”Auto”/>

    <RowDefinition Height=”Auto”/>

    <RowDefinition Height=”Auto”/>

    <RowDefinition Height=”Auto”/>

    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>

    <ColumnDefinition Width=”Auto”/>

    <ColumnDefinition Width=”Auto”/>

    <ColumnDefinition Width=”Auto”/>

    <ColumnDefinition Width=”Auto”/>

    <ColumnDefinition Width=”Auto”/>

    <ColumnDefinition Width=”Auto”/>

    <ColumnDefinition Width=”Auto”/>

    </Grid.ColumnDefinitions>

</Grid>

<Grid x:Name=”YearView” Margin=”6,-3,7,6″ Visibility=”Collapsed” Grid.ColumnSpan=”3″ Grid.Row=”1″>

    <Grid.RowDefinitions>

    <RowDefinition Height=”Auto”/>

    <RowDefinition Height=”Auto”/>

    <RowDefinition Height=”Auto”/>

    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>

    <ColumnDefinition Width=”Auto”/>

    <ColumnDefinition Width=”Auto”/>

    <ColumnDefinition Width=”Auto”/>

    <ColumnDefinition Width=”Auto”/>

    </Grid.ColumnDefinitions>

</Grid>

</Grid>

</Border>

</Border>

<Rectangle x:Name=”DisabledVisual” Fill=”{StaticResource DisabledBrush}” Stretch=”Fill” Stroke=”{StaticResource DisabledBrush}” StrokeThickness=”1″ RadiusX=”2″ RadiusY=”2″ Margin=”0,2,0,2″ Opacity=”0″ Visibility=”Collapsed”/>

                        </Grid>

                    </ControlTemplate>

                </Setter.Value>

            </Setter>

        </Style>

<Style x:Key=”MyCalendarDayButtonStyle” TargetType=”System_Windows_Controls_Primitives:CalendarDayButton”>

            <Setter Property=”Background” Value=”#FFBADDE9″/>

            <Setter Property=”FontSize” Value=”10″/>

            <Setter Property=”HorizontalContentAlignment” Value=”Center”/>

            <Setter Property=”VerticalContentAlignment” Value=”Center”/>

            <Setter Property=”MinWidth” Value=”5″/>

            <Setter Property=”MinHeight” Value=”5″/>

            <Setter Property=”Template”>

                <Setter.Value>

        <ControlTemplate TargetType=”System_Windows_Controls_Primitives:CalendarDayButton”>

                <Grid>

                <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″ Storyboard.TargetName=”Background” Storyboard.TargetProperty=”Opacity” To=”.5″/>

        </Storyboard>

        </VisualState>

        <VisualState x:Name=”Pressed”>

    <Storyboard>

    <DoubleAnimation Duration=”0″ Storyboard.TargetName=”Background” Storyboard.TargetProperty=”Opacity” To=”.5″/>

            </Storyboard>

            </VisualState>

            <VisualState x:Name=”Disabled”>

            <Storyboard>

    <DoubleAnimation Duration=”0″ Storyboard.TargetName=”Background” Storyboard.TargetProperty=”Opacity” To=”0″/>

    <DoubleAnimation Duration=”0″ Storyboard.TargetName=”Content” Storyboard.TargetProperty=”Opacity” To=”.35″/>

</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″ Storyboard.TargetName=”SelectedBackground” Storyboard.TargetProperty=”Opacity” To=”.75″/>

</Storyboard>

</VisualState>

</VisualStateGroup>

<VisualStateGroup x:Name=”CalendarButtonFocusStates”>

<VisualStateGroup.Transitions>

<VisualTransition GeneratedDuration=”0″/>

</VisualStateGroup.Transitions>

<VisualState x:Name=”CalendarButtonFocused”>

<Storyboard>

<ObjectAnimationUsingKeyFrames Duration=”0″ Storyboard.TargetName=”FocusVisual” Storyboard.TargetProperty=”Visibility”>

    <DiscreteObjectKeyFrame KeyTime=”0″ Value=”Visible”/>

    </ObjectAnimationUsingKeyFrames>

</Storyboard>

</VisualState>

<VisualState x:Name=”CalendarButtonUnfocused”>

<Storyboard>

    <ObjectAnimationUsingKeyFrames Duration=”0″ Storyboard.TargetName=”FocusVisual” Storyboard.TargetProperty=”Visibility”>

    <DiscreteObjectKeyFrame KeyTime=”0″ Value=”Collapsed”/>

    </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″ Storyboard.TargetName=”RegularDayGradientStart” Storyboard.TargetProperty=”Color” To=”#FF777777″/>

<ColorAnimation Duration=”0″ Storyboard.TargetName=”RegularDayGradientEnd” Storyboard.TargetProperty=”Color” To=”#FF777777″/>

</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″ Storyboard.TargetName=”TodayBackground” Storyboard.TargetProperty=”Opacity” To=”1″/>

    <DoubleAnimation Duration=”0″ Storyboard.TargetName=”TodayGradientEnd” Storyboard.TargetProperty=”Offset” To=”1″/>

    <DoubleAnimation Duration=”0″ Storyboard.TargetName=”RegularDayGradientStart” Storyboard.TargetProperty=”Offset” To=”1″/>

</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″ Storyboard.TargetName=”BlackoutVisual” Storyboard.TargetProperty=”Opacity” To=”.2″/>

</Storyboard>

</VisualState>

</VisualStateGroup>

</VisualStateManager.VisualStateGroups>

<Rectangle x:Name=”TodayBackground” Fill=”#FFB70CFA” RadiusX=”1″ RadiusY=”1″ Opacity=”0″/>

<Rectangle x:Name=”SelectedBackground” RadiusX=”1″ RadiusY=”1″ Opacity=”0″ Fill=”#FFEAE26A”/>

<Rectangle x:Name=”Background” RadiusX=”1″ RadiusY=”1″ Opacity=”0″ Fill=”#FFF2B1A8″/>

<ContentControl x:Name=”Content” HorizontalAlignment=”{TemplateBinding HorizontalContentAlignment}” Margin=”5,1,5,1″ VerticalAlignment=”{TemplateBinding VerticalContentAlignment}” FontSize=”{TemplateBinding FontSize}” IsTabStop=”False” Content=”{TemplateBinding Content}” ContentTemplate=”{TemplateBinding ContentTemplate}”>

<ContentControl.Foreground>

<LinearGradientBrush>

<GradientStop x:Name=”TodayGradientStart” Color=”#FFFFFFFF” Offset=”0″/>

<GradientStop x:Name=”TodayGradientEnd” Color=”#FFFFFFFF” Offset=”0″/>

<GradientStop x:Name=”RegularDayGradientStart” Color=”#FF333333″ Offset=”0″/>

<GradientStop x:Name=”RegularDayGradientEnd” Color=”#FF333333″ Offset=”1″/>

</LinearGradientBrush>

</ContentControl.Foreground>

</ContentControl>

<Path x:Name=”BlackoutVisual” Fill=”#FF000000″ Stretch=”Fill” HorizontalAlignment=”Stretch” Margin=”3″ VerticalAlignment=”Stretch” Opacity=”0″ RenderTransformOrigin=”0.5,0.5″ 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”/>

<Rectangle x:Name=”FocusVisual” Stroke=”Red” RadiusX=”1″ RadiusY=”1″ IsHitTestVisible=”false” Visibility=”Collapsed”/>

</Grid>

    </ControlTemplate>

</Setter.Value>

</Setter>

</Style>

</UserControl.Resources>

 

<Grid x:Name=”LayoutRoot” Background=”White”>

<controls:Calendar Margin=”27,8,0,0″ CalendarItemStyle=”{StaticResource MyCalendarItemStyle}” HorizontalAlignment=”Left” VerticalAlignment=”Top” CalendarDayButtonStyle=”{StaticResource MyCalendarDayButtonStyle}”/>

</Grid>