×

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!

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.


Gravatar
10/5/2009 11:27 AM | # re: Changing DatePicker in Silverlight to show current date
Thank you. This is really amazing.
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
10/5/2009 1:31 PM | # re: Changing DatePicker in Silverlight to show current date
Tim, I enjoyed your presentation this weekend. Thanks, Scott
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!
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.
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.
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.
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.
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/4/2009 6:05 AM | # re: Changing DatePicker in Silverlight to show current date
how to display the current date in DatePicker of Silverlight3
4/16/2010 4:32 AM | # re: Changing DatePicker in Silverlight to show current date
Nice work, it really sound nice
5/9/2010 3:07 PM | # re: Changing DatePicker in Silverlight to show current date
Once again a long post without answering the real question. How do you default the datepicker to the current date. I really couldn't care what number is in the little Icon. I do care how long it takes to research a simple question like setting the datepicker to today's date. A question that was never answered!
5/12/2010 7:07 AM | # re: Changing DatePicker in Silverlight to show current date
Lawrence, have you seriously been waiting a month for that question to be answered???

Just do something like: datePicker1.SelectedDate = DateTime.Now;

5/15/2010 9:54 AM | # re: Changing DatePicker in Silverlight to show current date
Lawrence, you're unbelievable. All you have to do is google "Silverlight Datepicker Default Date", and if you would have looked hard enough you would have found the answer in several places on page 1.

It's amazing a developer like you gets any project done without expecting people who are way too busy to hold you're hand to give you answers!

And to think you couldn't figure out programmically how to change the selected date as Damien showed you? Might want to go back to school for some additional training buddy.
2/27/2011 3:48 PM | # re: Changing DatePicker in Silverlight to show current date
Nice answer unbelievable! I totally sympathize with Lawrence as all too often jerks like you absolutely ruin a learning experience for a beginner. How many time have YOU wasted days looking for an succint answer to a reasonable question. I for one have been extremely disappointed with the answers I've found when searching by a topic. This is one of them. I too would like to know why the date in the datepicker shows 1/1/0001 some of the time and doesn't on other occasions. Using "datepicker1.SelectedDate=Datetime.Now" won't do you any good if you don't know where it goes, which using statements are required and so on. Hanging a snippet of code in a post with no other references is absolutely useless to someone who is in the early stages of learning something new and unknown. Please have a little consideration. By the way, I would NEVER hire someone with your attitude!
4/1/2011 11:34 PM | # re: Changing DatePicker in Silverlight to show current date
I had that in place I just went into OnApplyTemplate in DatePicker.cs and after the call to base.OnApplyTemplate() (line 851) I added bn visit bo visit bp visit bq visit
4/1/2011 11:40 PM | # re: Changing DatePicker in Silverlight to show current date
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 br visit bs visit bt visit bu visit
4/1/2011 11:49 PM | # re: Changing DatePicker in Silverlight to show current date
I could recompile the source and use my custom DatePicker in my application. The key difference in this modification is that bv visit bw visit bx visit by visit
4/1/2011 11:55 PM | # re: Changing DatePicker in Silverlight to show current date
I wanted this then I could recompile the source and use my custom DatePicker in my application. The key difference in this modification bz visit ca visit cb visit cc visit
4/2/2011 12:00 AM | # re: Changing DatePicker in Silverlight to show current date
to customize/extend exactly what you need as well as serving as a great learning resource for you in your control development cd visit ce visit cf visit cg visit
4/2/2011 12:05 AM | # re: Changing DatePicker in Silverlight to show current date
definition of Date Picker style/template. I changed the Path to a Content Presenter cd visit
5/10/2011 12:22 AM | # re: Changing DatePicker in Silverlight to show current date
Hi Tim,

I don't understand why the datepicker shows the datetime format string as the default watermark? First all the end-users are not expert in datetime format strings. By seeing the watermark <M/d/yyyy> they may get scared by the angular brackets and think why 'M' is in caps? There may be ways to display the watermark as m/d/yyyy (for my req.) but why we need such a difficulty in doing that? Why don't the development community include a basic property called WatermarkText? If they can't able to do that atleast help us how to hide the watermark in an easy way.
6/10/2011 8:38 PM | # re: Changing DatePicker in Silverlight to show current date
Amazing.. this make my day...
Tim Thanks a lot.
7/6/2011 6:52 AM | # re: Changing DatePicker in Silverlight to show current date
Experienced this at Daily Deals Gold Coast

Here at daily deals Gold Coast site, date picker was actually done through Jquery.
Just a very light plugin that was just added to site and it looks very good and functioning well.
7/6/2011 6:57 AM | # re: Changing DatePicker in Silverlight to show current date
Experienced this at Daily Deals Gold Coast

Here at daily deals Gold Coast site, date picker was actually done through Jquery.
Just a very light plugin that was just added to site and it looks very good and functioning well.
Gravatar
7/19/2011 4:31 PM | # re: Changing DatePicker in Silverlight to show current date
Hi, thanks for the awesome post!

I've located the source files and applied the changes you've described above. Where I'm stuck is how to apply these changes to the toolkit so I can use them in my projects. I'm not a developer by trade, so I'm not sure how to "build" out the changes to the toolkit for use in projects.

Any help would be most appreciated!
Gravatar
7/19/2011 5:01 PM | # re: Changing DatePicker in Silverlight to show current date
In response to my previous post. I was able to figure this out.

In case anyone else has the same issue as I did, you need to open the proper solution "Silverlight.Controls.Sdk.sln", change the generic.xaml and datepicker.cs files in that solution, then build the solution. You'll then need to reload the reference to the dll in your project to your local System.Windows.Controls.dll.

If there's a better solution to this, would someone else correct me? This worked for me, so I thought I'd share.

Thanks again for the awesome DatePicker fix!
7/20/2011 3:04 PM | # re: Changing DatePicker in Silverlight to show current date
I've been having some problems trying to get the date picker control to function properly. It has been driving me nuts for a while. call centers in miami
9/7/2011 11:41 PM | # re: Changing DatePicker in Silverlight to show current date
Thanks a lot for this beauty Enjoying article with me. I am Appreciating it very much! Looking Forward to Another Great.
eSports
10/9/2011 12:03 AM | # re: Changing DatePicker in Silverlight to show current date
This was a great article that really was really helpful to me and I really cant wait to learn more from your valuable experience. This was really very interesting to me. Aluminum Wallet

 
Please add 7 and 8 and type the answer here:

All postings/content on this blog are provided "AS IS" with no warranties, and confer no rights. All entries in this blog are my opinion and don't necessarily reflect the opinion of my employer or sponsors. The content on this site is licensed under a Creative Commons Attribution By license.