格式化 DatePicker 上显示的日期

Format date shown on DatePicker

我在 Visual Studio 2013 年创建了一个 Pivot 项目。

我创建了以下控件:

<DatePicker Header="Data" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0" Width="341"/>

当我在 phone 上点击它时,它显示日期为 M/D/Y。是否可以显示为D/M/Y?

此外,不知道我是否应该创建另一个问题,但我如何将控件中显示的 day/month 名称转换为 pt-BR?还有 "Choose a date".

DatePicker 格式使用用户的首选语言和区域。此控件会自动看起来不同,对于我所在的区域,它 day/month/year 就像您希望的那样。我试图强制控件使用其他语言,但它似乎是直接从 phone 设置中获取的。您可以在 MSDN:

找到一些信息

If you need to allow users to choose a date or select a time, use the standard date and time picker controls. These will automatically use the date and time formats for the user's preferred language and region.

你能找到的其他坏消息here at MSDN about formating that particular control:

Note This section applies to Windows Store apps using C++, C#, or Visual Basic. Formatting is ignored in Windows Phone Store apps.

因此在当前 API 中可能很难更改格式。

好消息是顶部的 CHOOSE DATE 文本 - 它会自动本地化,具体取决于用户的语言和地区。因此,如果您的应用程序支持用户的语言,您就不必担心。但我还没有找到将其更改为其他文本的方法。

关于点击按钮之前显示在按钮内的文本,您可以随时使用 ButtonDatePickerFlyout,一个简单的例子转换器:

<Button Content="{Binding ElementName=chosenDate, Path=Date, Converter={StaticResource DateFormatConverter}}">
    <Button.Flyout>
        <DatePickerFlyout x:Name="chosenDate" />
    </Button.Flyout>
</Button>

和转换器 class:

public class DateFormatConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        DateTimeOffset chosen = (DateTimeOffset)value;
        return string.Format("{0}/{1}/{2}", chosen.Day, chosen.Month, chosen.Year);
        // or use chosen.ToString() with format provider
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); }
}