0

I want to style the DateTimePicker from the Extended WPF Toolkit a bit differntly, especially the Calendar control.

<ControlTemplate x:Key="DatePickerTemplate" TargetType="{x:Type xctk:DateTimePicker}"> ... <Calendar x:Name="PART_Calendar" Template="{StaticResource CalendarTemplate}" BorderThickness="0" DisplayDate="2014-05-14"/>" ... </ControlTemplate> 

What is necessary so that I am allowed to reference the Control template below in the code above (both are in the same file)?

<ControlTemplate x:Key="CalendarTemplate" TargetType="Calendar"> <StackPanel x:Name="Root" HorizontalAlignment="Center"> <CalendarItem x:Name="CalendarItem" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Template="{StaticResource CalendarItemTemplate}"/> </StackPanel> </ControlTemplate> 

At present VS complains: The ressource "CalendarTemplate" could not be resolved.

Update: After changing the order of definitions, so first define <ControlTemplate x:Key="CalendarTemplate" TargetType="Calendar">... then the ControlTemplate that uses CalendarTemplate it works, remembers me of c++, but now I get the following Exception if I try to open the calendarcontrol:

Add value to dictionary of type 'System.Windows.ResourceDictionary' threw an exception. 
3
  • Is there an InnerException for the "Add value to dictionary of type 'System.Windows.ResourceDictionary' threw an exception." you are seeing? It could be that you have the same x:Key being declared more than once... Commented Jul 9, 2015 at 12:52
  • no it says: key can not be null Commented Jul 9, 2015 at 12:53
  • Then you must be declaring a ControlTemplate or some other type in your Resources section and not setting x:Key Commented Jul 9, 2015 at 12:55

2 Answers 2

2

You are referring to a StaticResource so you should be declaring your CalendarTemplate as it's x:Key not it's x:Name i.e.

<ControlTemplate x:Key="CalendarTemplate" TargetType="Calendar"> ... </ControlTemplate> 

Update to show something similar working (note that these are defined at the same level in my Xaml file i.e. both directly under my main Views Resources)

<ControlTemplate x:Key="ABC123" TargetType="Button"> </ControlTemplate> <!-- creating a control template for TextBox --> <ControlTemplate x:Key="PasswordBoxTemplateBase" TargetType="{x:Type PasswordBox}"> <Grid x:Name="root" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}"> <Border x:Name="normalBorder" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/> <Button Template="{StaticResource ABC123}"/> </Border> </Grid> </ControlTemplate> 
Sign up to request clarification or add additional context in comments.

6 Comments

my appologies, error while coping, updated the question, in the real code i use x:key not x:Name
@lsteinme are you still seeing the issue?
yes I still have the issue, the mistake with name/key happend while copying
@Isteinme are there any other build errors you are seeing? I currently have a single Xaml file with 2 ControlTemplates defined (I will add it into my answer to make it easier to read) and it is building and running fine
Marked as anwers, because the orginal problem was because of the wrong order
|
0

The issue is the use of the x:Name on a StaticResource rather than an x:Key. It should be like this (x:Key="CalendarTemplate"):

<ControlTemplate x:Key="CalendarTemplate" TargetType="Calendar"> </ControlTemplate> <ControlTemplate x:Key="DatePickerTemplate" TargetType="DatePicker"> <Calendar x:Name="PART_Calendar" Template="{StaticResource CalendarTemplate}" BorderThickness="0"/> </ControlTemplate> 

1 Comment

mistake while retyping the code, already corrected in the question

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.