How to change DatePicker default 'level'?
When using the Silverlight DatePicker, it opens on days view by default. When a user has to select his birthday, it would be a lot user-friendly if he could first select the year and drill down immediately without going up the tree first. I can guess that about 70% of our users won't even figure out how to use the calendar headers to go 'up', so if I could avoid this, it would be lovely.
Is there a way to manipulate which level the datepicker opens on by default?
Is there more us开发者_运维技巧er friendly control in Silverlight to select birthday?
On the face of it you should be able to do something like this:-
<sdk:DatePicker Name="datePicker1" >
<sdk:DatePicker.CalendarStyle>
<Style TargetType="sdk:Calendar">
<Setter Property="DisplayMode" Value="Decade" />
</Style>
</sdk:DatePicker.CalendarStyle>
</sdk:DatePicker>
For pretty much all other features of the calendar displayed in the dropdown this would work. However code internal to the DataPicker will set DisplayMode
to Month
whenever the dropdown is opened. So we need to resort to an ugly hack:-
private void datePicker1_CalendarOpened(object sender, RoutedEventArgs e)
{
DatePicker picker = (DatePicker)sender;
if (!picker.SelectedDate.HasValue)
{
var popup = picker.Descendents().OfType<Popup>().FirstOrDefault();
if (popup != null)
{
var item = popup.Child.Descendents().OfType<Calendar>().FirstOrDefault();
if (item != null)
item.DisplayMode = CalendarMode.Decade;
}
}
}
See this answer for my implementation of the Descendents
extension method.
精彩评论