Show only month and year in DatePicker WP8

4.8k views Asked by At
    <toolkit:DatePicker Value="2/28/2010" />

I need show only month and year in my date picker (WP8). I've tried to do this in such way, but it was not working:

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

I need it for DatePickerPage too.

5

There are 5 answers

0
Rakesh R Nair On BEST ANSWER

You cannot implement this by adding xaml string format. For implement such functionality you need to edit the toolkit code. For that you can download the open source code from CodePlex. After downloading the project find the Date Picker class and edit the relevant methods to remove the date. recompile the source in release mode and you can use that new dll.

4
MatDev8 On

{}{0:"Y","y"} have a good day :D ( information here --> http://msdn.microsoft.com/en-us/library/az4se3k1%28v=VS.95%29.aspx)

0
Dzianis Prokharchyk On

Format exhibited depending on the location but this can be avoided by writing this:

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

And you will be happy!(dd'/'MM'/'yyy)

0
user2959478 On

This should help to format the DateTimeButton content to display month and year only: <toolkit:DatePicker x:Name="datePicker" ValueStringFormat="{}{0:y}"/>

0
LukeChastain On

If you don't want to copy the page from Codeplex, you can extend the page and override the GetSelectorsOrderedByCulturePattern method to change the day selector's visibility. In the example below I've fixed it to month then year, which you may not want in your version. Also I'm accessing controls by name, so take care with future updates to 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>

And

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;
        }
    }
}