Extending DatePicker problem

8 posts, 1 answers
  1. haagel
    haagel avatar
    52 posts
    Member since:
    Feb 2010

    Posted 12 Jan 2011 Link to this post

    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?
  2. Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 13 Jan 2011 Link to this post

    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>>
  3. UI for WPF is Visual Studio 2017 Ready
  4. haagel
    haagel avatar
    52 posts
    Member since:
    Feb 2010

    Posted 14 Jan 2011 Link to this post

    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?
  5. Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 14 Jan 2011 Link to this post

    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>>
  6. haagel
    haagel avatar
    52 posts
    Member since:
    Feb 2010

    Posted 19 Jan 2011 Link to this post

    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?
  7. Answer
    Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 21 Jan 2011 Link to this post

    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>>
  8. haagel
    haagel avatar
    52 posts
    Member since:
    Feb 2010

    Posted 24 Jan 2011 Link to this post

    Thanks Kaloyan, that works fine. :)
  9. prithu
    prithu avatar
    8 posts
    Member since:
    Aug 2012

    Posted 08 Feb 2012 Link to this post

    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
Back to Top
UI for WPF is Visual Studio 2017 Ready