在DatePicker WP8中仅显示月份和年份

时间:2013-09-12 10:06:30

标签: windows-phone-8 datepicker windows-phone-toolkit

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

我需要在我的日期选择器(WP8)中仅显示月份和年份。我试图以这种方式做到这一点,但它无法正常工作:

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

我也需要DatePickerPage。

5 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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;
        }
    }
}

答案 2 :(得分:0)

{} {0:“Y”,“y”}有一个美好的一天:D(此处的信息 - &gt; http://msdn.microsoft.com/en-us/library/az4se3k1%28v=VS.95%29.aspx

答案 3 :(得分:0)

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

答案 4 :(得分:0)

根据位置展示格式,但可以通过以下方式避免这种情况:

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

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

相关问题