WPF custom control style problem

controls, styles, wpf

Solution

have you tried changing the TargetType to:

TargetType="{x:Type toolkit:DatePicker}"> 

You are referencing local in one place and toolkit in another.

update: I've tried it in a small app. This is the xaml as it should work:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WpfApplication3.Window1"
    x:Name="Window"
    Title="Window1"
    Width="640" Height="480"
    xmlns:Toolkit="http://schemas.microsoft.com/wpf/2008/toolkit">
  <Window.Resources>
    <Style TargetType="{x:Type Toolkit:DatePicker}">
      <Setter Property="Background" Value="#FFFF0000"/>
    </Style>
  </Window.Resources>
  <Grid x:Name="LayoutRoot">
    <Toolkit:DatePicker HorizontalAlignment="Left" 
                        Margin="61,143,0,116" Width="232" />
  </Grid>
</Window>

This example should create a datepicker with a red background.

Problem

I have a custom control (from MS Toolkit - DatePicker). I've made my own style like this: ``` <Style TargetType="{x:Type local:DatePicker}"> ``` But this style does not apply automatically. I have to add Key: ``` <Style x:Key="DatePickerStyle" TargetType="{x:Type local:DatePicker}"> ``` and reference it in each custom control like ``` <toolkit:DatePicker Style="{StaticResource DatePickerStyle}" ``` ... to get it working. Does anyone know why?

Original source