Shawn
Shawn

Reputation: 5260

wpf change calendar day title foreground color

I am trying to change calendar day title foreground color. I am using standard .net 4.0 Datepicker. The calendar is embedded in the Datepicker.

I have the following code in the resource file. but it does not work.

<Style  TargetType="{x:Type CalendarItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type CalendarItem}">
                <ControlTemplate.Resources>
                    <DataTemplate x:Key="DayTitleTemplate">
                        <TextBlock
                                            FontWeight="Bold" 
                                            FontFamily="Verdana" 
                                            FontSize="9.5" 
                                            Foreground="Red" 
                                            HorizontalAlignment="Center"
                                            Text="{Binding}"
                                            Margin="0,6,0,6"
                                            VerticalAlignment="Center"/>
                    </DataTemplate>
                </ControlTemplate.Resources>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Can anyone help see what I am missing here? Thanks,

Upvotes: 3

Views: 5178

Answers (2)

Mark Hall
Mark Hall

Reputation: 54532

Take a look at this MSDN Article, it is about customizing the WPF Calendar controls and has this excerpt on the DatePicker Control.

From Link (emphasis mine):

But all the styles and templates you can apply to the standalone Calendar control can also be applied to the Calendar control invoked from the dropdown in DatePicker. The DatePicker control has a property named CalendarStyle of type Style, and the Style object you set to this property can contain setters for any property defined by Calendar, including the CalendarItemStyle, CalendarButtonStyle, and CalendarDayButtonStyle properties.

See this link for the DatePicker Template.

Upvotes: 0

McGarnagle
McGarnagle

Reputation: 102743

Unfortunately the foreground property is hard-coded in the default control template. The way to change it is to copy-and-modify the template.

<Style x:Key="CalendarItemStyle" TargetType="{x:Type CalendarItem}">
    <Setter Property="Margin" Value="0,3,0,3" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type CalendarItem}">
                    <ControlTemplate.Resources>
                        <DataTemplate x:Key="{x:Static CalendarItem.DayTitleTemplateResourceKey}">
                            <TextBlock 
                                <!-- Day header color here -->
                                Foreground="Red"
                                FontWeight="Bold"
                                FontSize="9.5"
                                FontFamily="Verdana"
                                Margin="0,6,0,6"
                                Text="{Binding}"
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center" />
                        </DataTemplate>

You also might want to use Foreground="{TemplateBinding}", so that way you can modify the color by setting the Foreground property on individual controls.

Upvotes: 1

Related Questions