开发者

Custom date format for WPF Calendar CalendarItems

开发者 https://www.devze.com 2023-01-06 17:10 出处:网络
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-开发者_Go百科CA culture) are displayed as \"Jan\", \"Feb\",

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-开发者_Go百科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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号