Advertisement

Changing DatePicker in Silverlight to show current date

This past weekend during a conversation about Silverlight controls at the San Diego Silverlight Education Day, someone asked about the DatePicker control and why the “15” is there and how come they can’t change it.  Here’s the control that was being referred to:

Silverlight DatePicker default

The calendar displays an icon to the right of the text box area which, when clicked, displays a drop-down calendar picker.  The icon always says “15” and is intended to look like a little calendar date sheet (you know, like those desk calendars).

The question was how to change that icon.  Well, easily.  You can easily edit the template using Blend and drill into the UI parts and change it.  I opened Blend, chose to edit the template, then edit the Button template and you’ll see that the “15” is actually a Path:

DatePicker custom template in Blend

Now that I know where that template is, I can put anything i want there to replace the Path data to anything I want.  But the question was raised as to why it doesn’t show the current date text instead.  So on the plane ride back I did a quick refactor to see how easy that could be done.

After getting the Ms-PL licensed source code for the DatePicker control from the Silverlight Toolkit, I had access to the full source of the control to get the inner guts.  Here’s what I did. 

First, I changed generic.xaml (located in Themes folder) definition of DatePicker style/template.  I changed the Path to a ContentPresenter (about line 437 in the generic.xaml file).  I removed the Path and replaced it with this:

   1: <ContentPresenter x:Name="DropDownDateText" HorizontalAlignment="Center" Margin="0,0,0,0" 
   2:     VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" Grid.Column="0" Grid.Row="1" 
   3:     Grid.ColumnSpan="4" Grid.RowSpan="3"/>

Once I had that in place I just went into OnApplyTemplate in DatePicker.cs and after the call to base.OnApplyTemplate() (line 851) I added some additional code:

   1: // change the date to the current date
   2: string DateDay = DateTime.Now.Day.ToString(CultureInfo.CurrentUICulture);
   3:  
   4: // get the Button template and insert the content as current date text
   5: Button b = this.GetTemplateChild(DatePicker.ElementButton) as Button;
   6: TextBlock tb = new TextBlock();
   7: tb.FontFamily = new FontFamily("Arial Bold");
   8: tb.FontSize = 9.5;
   9: tb.FontStretch = FontStretches.Expanded;
  10: tb.UseLayoutRounding = true;
  11: tb.Text = DateDay;
  12: b.Content = tb;

What this does is basically use the local time information to get the current Day text and put that TextBlock as the Content for the button…the result is that the DatePicker now will display the number for the current Date (I took this screenshot on 05 OCT 2009):

Custom DatePicker with current date text

So if I wanted this then I could recompile the source and use my custom DatePicker in my application.  The key difference in this modification is that I’m not using a Path, but a TextBlock.

I’m not sure if this is useful, but since the question was posed and I had time to kill, I thought I’d post my findings.  Having access to the source control for all the Silverlight controls is a great way to customize/extend exactly what you need as well as serving as a great learning resource for you in your control development.

Hope this helps!


This work is licensed under a Creative Commons Attribution By license.

  1. Gravatar
    10/5/2009 11:27 AM | # re: Changing DatePicker in Silverlight to show current date
    Thank you. This is really amazing.
  2. 10/5/2009 1:13 PM | # re: Changing DatePicker in Silverlight to show current date
    Tim, thanks for the post. If you're interested I wrote a post on this a little while ago. It uses the first technique you described (using Blend to drill into the template) and is a 100% xaml solution. However, something that still bugs me to this day about my solution is that the template binding I'm uses causes an error in Blend, but works fine at runtime. I'd be curious (and grateful) if you had the time to look at it and tell me if it is something that can be fixed on my end. Here's the link: www.developmentalmadness.com/.../...r-control.aspx
  3. 10/5/2009 1:31 PM | # re: Changing DatePicker in Silverlight to show current date
    Tim, I enjoyed your presentation this weekend. Thanks, Scott
  4. 10/5/2009 1:39 PM | # re: Changing DatePicker in Silverlight to show current date
    Mark -- NICE! I'll see if I can take a look on the binding issue. But very cool control!
  5. 10/6/2009 2:17 AM | # re: Changing DatePicker in Silverlight to show current date
    @Mark: nice!

    @Tim: this possibly opens up more goodness for this particual control. Perhaps the displaying of weeknumbers etc. etc.
  6. 10/6/2009 4:39 AM | # re: Changing DatePicker in Silverlight to show current date
    And how does it work when you want to type the date in - does it have a mask? does it validate it's a date ?

    I have yet to meet a data entry person who wants to click on a date picker and select a date - they can type it in very quickly. When you show a datetime hint, the user will expect that control to be entered without entering slashes and will want to make sure it's a validate datetime. This is usability. ( encosia.com/.../is-silverlight-the-new-webforms/ )

    Cute and clever but not very usable for real business applications.

    I was told Blend was targeted for designers - but it appears that Blend is an essential Developer tool. Too bad it's a separate expensive application outide of Visual Studio.
  7. 10/6/2009 6:23 AM | # re: Changing DatePicker in Silverlight to show current date
    Steve - not sure if you were directing your comments to Tim or myself, neither version has a mask, but Tim's and the original allow you to type the date in. Mine does not. But it's about knowing your users - mine aren't data entry professionals, and many aren't even real computer savvy which means the tab button generally doesn't exist for them. Also, I disagree about the users *expecting* a mask. I've never seen a mask work well in a web application and I've only seen it tried very rarely. So I actually think the users would not expect a mask. However, it would be an interesting exercise to add masking to the control - I would think ignoring anything but numbers would be the key.
  8. 10/12/2009 10:34 AM | # re: Changing DatePicker in Silverlight to show current date
    Tim, thanks. I got a comment on my post helping me fix the problem I was referring to. Just, fyi.
  9. 10/30/2009 3:50 AM | # re: Changing DatePicker in Silverlight to show current date
    Nice post. After recompiling the source should I replace the Window.System.Controls.dll generated in the /obj/debug folder in the folder where it was after the installation of the Silverlight?
  10. 10/30/2009 9:03 AM | # re: Changing DatePicker in Silverlight to show current date
    Lucian - if you are creating a new assembly you should give it a different name ideally (as not to conflict) and then add a reference in your project to it. That will copy the assembly in the right places.
  11. 11/4/2009 6:05 AM | # re: Changing DatePicker in Silverlight to show current date
    how to display the current date in DatePicker of Silverlight3
  12. 12/30/2009 2:27 PM | # re: Changing DatePicker in Silverlight to show current date
    Hey Tim,
    That was me who asked the question, so it's funny I didn't see this until now. The training was excellent and it really helped jump start Silverlight development for me. Shortly after the training, I gave you a shout out in a blog post on SharePoint / Silverlight integration as you had also answered a question about that.

    johnlivingstontech.blogspot.com/.../...epoint.html

    johnlivingstontech.blogspot.com/.../...er-web.html

    Happy Festivus,
    John


 
Please add 3 and 6 and type the answer here:
First time here? You are looking at the most recent posts. You may also want to check out older archives. Please leave a comment, ask a question and consider subscribing to the latest posts via RSS or email. Thank you for visiting! (hide this)