Momo
Momo

Reputation: 123

CalendarStyle of DatePicker different than my custom CalendarStyle

I have a custom Calendar style and a custom DatePicker style.

Code in my MyCustomSkin.xaml (from my library project)

<Style TargetType="{x:Type Calendar}">
    <Setter Property="Background" Value="Red"/>
</Style>
<Style TargetType="{x:Type DatePicker}">
    <Setter Property="Background" Value="Green"/>
</Style>

They are both inside my MyCustomSkin.xaml ResourceDictionary (separate solution that generate a MyCustomSkin.dll) that I reference inside my application.

Code in my App.xaml (from my application)

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MyCustomSkin;component/MyCustomSkin.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Code in my MainWindow.xaml (from my application)

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow">
    <Grid>
        <DatePicker HorizontalAlignment="Right" />
        <Calendar HorizontalAlignment="Left"/>
    </Grid>
</Window>

When I create a Calendar in my application (without specifying any style), the background is red. When I am creating a DatePicker in my application (without specifying any style), the background is green but the background of its calendar is the system one (=> not red).

Why? I thougt that I was overriding all the system calendars with mine. Why is it applying the custom style for the Calendar but not on the Calendar of DatePicker?

Thank you!

Upvotes: 0

Views: 1044

Answers (1)

stratever
stratever

Reputation: 596

Your MyCustomSkin.xaml should be:

<Style TargetType="{x:Type Calendar}" >
                <Setter Property="Background" Value="Red" />
            </Style>

            <Style TargetType="{x:Type DatePicker}" >
                <Setter Property="Background" Value="Green" />
                <Setter Property="CalendarStyle">
                    <Setter.Value>
                        <Style TargetType="{x:Type Calendar}" BasedOn="{StaticResource {x:Type Calendar}}"/>

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

DatePicker has a default CalendarStyle which will be applied on component initialization.

Upvotes: 2

Related Questions