0
    <toolkit:DatePicker Value="2/28/2010" />

我只需要在我的日期选择器(WP8)中显示月份和年份。我试图以这种方式做到这一点,但它不起作用:

    <toolkit:DatePicker Value="{Binding DateValue}" ValueStringFormat="{}{0:MM-yyyy}" />

我也需要它用于 DatePickerPage。

4

5 回答 5

2

您不能通过添加 xaml 字符串格式来实现这一点。要实现此类功能,您需要编辑工具包代码。为此,您可以从CodePlex下载开源代码。下载项目后,找到 Date Picker 类并编辑相关方法以删除日期。在发布模式下重新编译源代码,您就可以使用新的 dll。

于 2013-09-12T12:19:04.817 回答
1

如果您不想从 Codeplex 复制页面,则可以扩展页面并覆盖 GetSelectorsOrderedByCulturePattern 方法以更改日期选择器的可见性。在下面的示例中,我已将其固定为月和年,您可能不希望在您的版本中使用它。此外,我正在按名称访问控件,因此请注意未来对 WindowsPhone Toolkit 的更新。

<toolkit:DatePickerPage
    x:Class="MyApp.MonthYearPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
    mc:Ignorable="d"
    shell:SystemTray.IsVisible="True">

</toolkit:DatePickerPage>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Controls.Primitives;
using System.Diagnostics;
using System.Windows.Media;

namespace MyApp
{
    public partial class MonthYearPage : DatePickerPage
    {
        public MonthYearPage()
        {
            InitializeComponent();
        }

        protected override IEnumerable<LoopingSelector> GetSelectorsOrderedByCulturePattern()
        {
            var PrimarySelector = ((Grid)Content).FindName("PrimarySelector") as LoopingSelector;
            var SecondarySelector = ((Grid)Content).FindName("SecondarySelector") as LoopingSelector;
            var TertiarySelector = ((Grid)Content).FindName("TretiarySelector") as LoopingSelector;

            var selectors = GetSelectorsOrderedByCulturePattern(
                "DMY",
                new char[] { 'Y', 'M', 'D' },
                new LoopingSelector[] { PrimarySelector, SecondarySelector, TertiarySelector });

            // Passing "DMY" in on the previous line makes sure the day selector
            // is the first one in the enumeration. You may want to change the order
            // of 'M' and 'Y' in that string for your needs or depending on region.
            selectors.First<LoopingSelector>().Visibility = Visibility.Collapsed;
            return selectors;
        }
    }
}
于 2014-03-24T17:12:56.800 回答
0

这应该有助于将 DateTimeButton 内容格式化为仅显示月份和年份: <toolkit:DatePicker x:Name="datePicker" ValueStringFormat="{}{0:y}"/>

于 2013-11-06T08:07:19.677 回答
0

{}{0:"Y","y"} 祝您有美好的一天 :D (此处的信息 --> http://msdn.microsoft.com/en-us/library/az4se3k1%28v=VS.95%29 .aspx )

于 2013-09-12T11:02:14.467 回答
0

显示的格式取决于位置,但这可以通过编写以下内容来避免:

<toolkit:DatePicker 
                    Value="{Binding DateCard, Mode=TwoWay}" 
                    ValueStringFormat="{}{0:MM'/'yy}" />

你会很开心!(dd'/'MM'/'yyy)

于 2015-08-11T14:12:25.473 回答