views:

16

answers:

1

I plan to use a WPF Calendar control to display fiscal month/years. When I set the control's DisplayMode to "Year", the months (for my en-CA culture) are displayed as "Jan", "Feb", etc. I'd like to display them as "01", "02", etc but can't find a way to do this.

+1  A: 

From Reflector, the CalendarItem.SetYearModeMonthButtons() method has code like this:

CalendarButton childButton = child as CalendarButton;
DateTime day = new DateTime(this.DisplayDate.Year, count + 1, 1);
childButton.DataContext = day;
childButton.SetContentInternal(DateTimeHelper.ToAbbreviatedMonthString(new DateTime?(day), DateTimeHelper.GetCulture(this)));
childButton.Visibility = Visibility.Visible;

ToAbbreviatedMonthString uses DateTimeFormatInfo.AbbreviatedMonthNames and gets the CultureInfo from the XmlLanguage returned by FrameworkElement.Language, and I don't think there is a way to create a custom XmlLanguage to fake out the month names.

So your only option seems to be to change the control template for the CalendarButton. You can do something like this:

<toolkit:Calendar DisplayMode="Year">
    <toolkit:Calendar.CalendarButtonStyle>
        <Style TargetType="primitives:CalendarButton">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="primitives:CalendarButton">
                        <primitives:CalendarButton>
                            <TextBlock Text="{Binding Month, StringFormat=00}"/>
                        </primitives:CalendarButton>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </toolkit:Calendar.CalendarButtonStyle>
</toolkit:Calendar>

That will also affect the year buttons, which will now all show '01' (since they all represent January 1), and doesn't highlight the current month, but it should give you a start if you want to take that approach.

Quartermeister