sadrick
sadrick

Reputation: 13

Calendar style doesn`t affect DatePicker calender

I have added the following style resource to customize the appearance of my DatePicker calendar. The Calendar is also affected but not the calendar of the DatePicker. See attached image.

<Style TargetType="{x:Type Calendar}">
    <Setter Property="Foreground" Value="#FF333333"/>
    <Setter Property="Background">
        <Setter.Value>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="Orange" Offset="0"/>
                <GradientStop Color="Orange" Offset="0.16"/>
                <GradientStop Color="White" Offset="0.16"/>
                <GradientStop Color="White" Offset="0.4"/>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
    <Setter Property="BorderBrush">
        <Setter.Value>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="Orange" Offset="0"/>
                <GradientStop Color="Orange" Offset="0.375"/>
                <GradientStop Color="Orange" Offset="0.375"/>
                <GradientStop Color="Orange" Offset="1"/>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Calendar}">
                <StackPanel x:Name="PART_Root" HorizontalAlignment="Center">
                    <CalendarItem x:Name="PART_CalendarItem" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Style="{TemplateBinding CalendarItemStyle}"/>
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Calendar Style

Upvotes: 0

Views: 169

Answers (1)

BionicCode
BionicCode

Reputation: 28968

When looking at the original default Style of the DatePicker you would see that the Style for the Calendar is explicitly assigned to the dedicated DatePicker.CalendarStyle. This makes sense as the intention is to restrict the Style to the Calendar of the DatePicker. It should not apply to the Calendar control in general.

So you must override the explicit default Style.
Given that your implicit Style is within the scope of the DatePicker, you simply have to set the DatePicker.CalendarStyle property to reference it:

<DatePicker CalendarStyle="{StaticResource {x:Type Calendar}}" />

Upvotes: 2

Related Questions