Calendardatepicker日期格式

时间:2018-12-07 09:59:53

标签: c# xaml uwp

是否可以使用Calendardatepicker样式/模板设置日期格式?

  <Style x:Name="CalendarDatePicker" TargetType="CalendarDatePicker">

    <Setter Property="Foreground" Value="Green" />
    <Setter Property="Margin" Value="5,0,5,0" />
    <Setter Property="Margin" Value="5,0,5,0" />
    <Setter Property="HorizontalAlignment" Value="Stretch" />
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="DateFormat" Value="{}{day.integer}‎.‎{month.integer}‎.{year.full}" />

</Style>

我尝试如上所述进行操作,但是它不起作用。

1 个答案:

答案 0 :(得分:0)

我不为什么采用这种方式设计CalendarDatePicker,仅当DateFormat的“ ReadLocalValue”为true时才接受DateFormat。 这意味着以下两个更改是有效的:

<CalendarDatePicker x:Name="myCalendarDatePicker"
                    DateFormat = "{}{dayofweek.full}‎, ‎{month.full}‎ ‎{day.integer}‎, ‎{year.full}"/>

myCalendarDatePicker.DateFormat = "{dayofweek.full}‎, ‎{month.full}‎ ‎{day.integer}‎, ‎{year.full}";

所以它不适用于您的示例。您可以通过创建嵌套的虚拟CalendarDatePicker来解决此问题,但我不建议您这样做。

  <Style x:Key="CalendarDatePicker" TargetType="CalendarDatePicker">
        <Setter Property="Foreground" Value="Green" />
        <Setter Property="Margin" Value="5,0,5,0" />
        <Setter Property="Margin" Value="5,0,5,0" />
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="DateFormat" Value="{}{day.integer}‎.‎{month.integer}‎.{year.full}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="CalendarDatePicker">
                    <Grid x:Name="RootElement">
                        <CalendarDatePicker DateFormat="{TemplateBinding DateFormat}"
                                            Foreground="{TemplateBinding Foreground}"
                                            Margin="{TemplateBinding Margin}"
                                            HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                                            VerticalAlignment="{TemplateBinding VerticalAlignment}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>