This is a migrated thread and some comments may be shown as answers.

Extending DatePicker problem

7 Answers 88 Views
DatePicker
This is a migrated thread and some comments may be shown as answers.
haagel
Top achievements
Rank 1
haagel asked on 12 Jan 2011, 09:41 AM
I have the need to extend the DatePicker. I try to do this as I have done when I have extended the standard WPF controls (e.g. TextBox), but I have some problems. I can't make my extended control use its own template.

The control has this code:
public class DatePickerExtended : RadDatePicker
{
    static DatePickerExtended()
    {
        //Make sure the style in Themes/Generic.xaml is used.
        DefaultStyleKeyProperty.OverrideMetadata(typeof(DatePickerExtended), new FrameworkPropertyMetadata(typeof(DatePickerExtended)));
    }
}

Then I have a folder in the root called "Themes" which contains a file called "Generic.xaml". This XAML file contains the template that i want to apply to the control:
<Style TargetType="{x:Type local:DatePickerExtended}" x:Key="{x:Type local:DatePickerExtended}">
    <!-- A bunch of setters, including for the Template property. -->
</Style>

But the template is never applied. Instead the default template for the DatePicker is used, and I don't now how to replace this with my own...

So the question is: How can I make my custom control inheriting from DatePicker use a custom template?

7 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 13 Jan 2011, 03:26 PM
Hi David Haglund,

Everything you have done is correct. In addition make sure that the style is in a Generic.xaml file under a Theme folder in your project. You can also try to include the declared style in the app resources. Let us know if this is not helping you.

Greetings,
Kaloyan
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
haagel
Top achievements
Rank 1
answered on 14 Jan 2011, 01:03 PM
I have a folder called "Themes" in the root containing "Generic.xaml". And this file works fine for another control I'm extending (standard TextBox). But my style/template for RadDatePicker just won't apply! Do you have any test project where you have succesfully extended the RadDatePicker and given it another template?
0
Kaloyan
Telerik team
answered on 14 Jan 2011, 03:50 PM
Hello David Haglund,

Find the attached sample.

Best wishes,
Kaloyan
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
haagel
Top achievements
Rank 1
answered on 19 Jan 2011, 06:09 PM
Well...that works. But that's an ordinary default style that you make a reference to in App.xaml. My point is that when I've extended the standard controls (such as TextBox) I've always just put my own default style/template in a file called "Themes\Generic.xaml", and then it gets applied automaitcally without me having to make references to it anywhere. That's the whole point with using Generic.xaml; otherwise I could put it in any XAML-resource file.

In your example project you do have the codeline that hooks up the extended control to it's default style/template, but that doesn't actually do anything since there is no Themes-folder. So I can remove the codeline and it doesn't matter.

By using the Generic.xaml approach I can put all my extended controls and their templates in one assembly and use them in as many other assemblies as I like, without having to make references to any styles.

Is it not possible to do this when extending Telerik controls?
0
Accepted
Kaloyan
Telerik team
answered on 21 Jan 2011, 02:27 PM
Hello David Haglund,

In that case you have to override the OnInitialized method as well:

protected override void OnInitialized(System.EventArgs e) 
      base.OnInitialized(e);  this.DefaultStyleKey = typeof(DatePickerExtended); 
}

 
Greetings,
Kaloyan
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
haagel
Top achievements
Rank 1
answered on 24 Jan 2011, 12:00 PM
Thanks Kaloyan, that works fine. :)
0
prithu
Top achievements
Rank 1
answered on 08 Feb 2012, 11:17 AM
Hi David and Kaloyan,
                                    I have the exact problem as David. The style in generic.xaml is never applied. When I copy paste the same style in App.xaml the style is applied the 2nd time the control is used. The fist time the control is displayed with default style.

Hi David.. were u able to solve the problem?

Best regards,
prithu
Tags
DatePicker
Asked by
haagel
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
haagel
Top achievements
Rank 1
prithu
Top achievements
Rank 1
Share this question
or