【问题标题】:Show only month and year in DatePicker WP8在 DatePicker WP8 中仅显示月份和年份
【发布时间】:2013-09-12 10:06:30
【问题描述】:
    <toolkit:DatePicker Value="2/28/2010" />

我只需要在日期选择器 (WP8) 中显示月份和年份。我曾尝试以这种方式执行此操作,但它不起作用:

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

我也需要它用于 DatePickerPage。

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      如果您不想从 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;
              }
          }
      }
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 此类代码未编译。 :(
        • 抱歉尝试 ValueStringFormat="{}{0:Y,y}" 或 ValueStringFormat="{}{0:Y:y}"...我无法在我的电脑上进行测试对不起。如果它不起作用,我今晚会测试它:)
        • 此类代码仅适用于 DatePicker,但我只需要 DatePickerPage 上的两个列。
        • 嗯,我认为您无法使用工具包删除列,但您可以看到此链接 geekchamp.com/articles/…
        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

          显示的格式取决于位置,但可以通过编写以下代码来避免这种情况:

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

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

          【讨论】:

            猜你喜欢
            • 2018-06-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-02-05
            • 1970-01-01
            • 2015-08-17
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多