WPF DatePicker format

22,527

Solution 1

All of the other answers seem a little bit convoluted. It is as simple as:

<DatePicker SelectedDate="{Binding PropertyName, StringFormat=dd/MM/yyyy}"/>

All you need to do is adjust the string format to suit yourself.

Solution 2

Use this snippet for your DatePicker (you will have to style it):

    <DatePicker SelectedDate="{Binding myVideModelProperty}">
        <DatePicker.Resources>
            <Style TargetType="{x:Type DatePickerTextBox}">
                <Setter Property="Control.Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <TextBox x:Name="PART_TextBox"
                                     Text="{Binding Path=SelectedDate, 
                                            RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}, 
                                            StringFormat={x:Static local:App.DateFormat}}" />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DatePicker.Resources>
    </DatePicker>

Use this snippet for your App.xaml.cs (change the namespace to something useful):

namespace _24417730
{
    public partial class App
    {
        public static string DateFormat = "yyyy/MM/dd";
    }
}

In the xaml control containing your DatePicker, define a XAML namespace like so: xmlns:local="clr-namespace:_24417730"

Note that you will have to change the namespace to the one you used to define your application class.

Here is how I defined my xaml namespace on the MainWindow.xaml control:

<Window x:Class="_24417730.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:_24417730"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <DatePicker SelectedDate="{Binding myVideModelProperty}">
            <DatePicker.Resources>
                <Style TargetType="{x:Type DatePickerTextBox}">
                    <Setter Property="Control.Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <TextBox x:Name="PART_TextBox"
                                                 Text="{Binding Path=SelectedDate, 
                                                        RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}, 
                                                        StringFormat={x:Static local:App.DateFormat}}" />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DatePicker.Resources>
        </DatePicker>
    </StackPanel>
</Window>

Also, reading up on Static Markup Extension and XAML Namespaces may help for future reference.

If you require any further clarifications, I will be happy to assist.

Solution 3

You can use converters for that. Or you can add another property to your class which will return the formatted date.

With converter

public class DateFormatConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return string.Format(Application.DateFormat, value);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
    }
}
Share:
22,527
Indu
Author by

Indu

Updated on July 09, 2022

Comments

  • Indu
    Indu almost 2 years

    I got this sample code from this forum.

    <DatePicker SelectedDate="{Binding myVideModelProperty}"
                    Height="25" HorizontalAlignment="Left" Margin="81,-2,0,0" Name="myDatePicker" VerticalAlignment="Top" Width="115">
            <DatePicker.Resources>
                <Style TargetType="{x:Type DatePickerTextBox}">
                    <Setter Property="Control.Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <TextBox x:Name="PART_TextBox" 
                                    Text="{Binding Path=SelectedDate, RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}, StringFormat={}{0:yyyy/MM/dd}}" />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DatePicker.Resources>
        </DatePicker>
    

    But I want the format i.e yyyy/MM/dd to come from an application property called dateformat. Can you help me put the stringformat as a binding to the application property.

  • Indu
    Indu almost 10 years
    It is defined as Static Property in Application.xaml.vb
  • Tyler Kendrick
    Tyler Kendrick almost 10 years
    Any of the answers on this page should address your problem. If you need clearer instruction, let me know and I will revise my initial answer with a more complete applicable example.
  • Indu
    Indu almost 10 years
    I am working on WPF for the first time. I got the error - ** Cannot find the type 'Application'. Note that type names are case sensitive. ** I have defined local like this. xmlns:local="clr-namespace:BillingApp" BillingApp is the root assembly namespace. Application.xaml.vb doesn't have any namespace defined in it.
  • Tyler Kendrick
    Tyler Kendrick almost 10 years
    I have added additional clarification on how to define and consume XAML namespaces. Hopefully, this should resolve your issue.
  • Indu
    Indu almost 10 years
    I added like this - Namespace BillingApp.Common Partial Class Application and I got error that Event 'Exit' cannot be found. I have some code written in the application.exit
  • Tyler Kendrick
    Tyler Kendrick almost 10 years
    It is likely that your DataContext isn't set in the Application.xaml. If you post the xaml portion of your code setting the binding to the exit event and tell me which file it is in, I can likely give you the solution.
  • Indu
    Indu almost 10 years
    This is my method is application.xaml.vb. I don't have any binding to this exit event which I have written. Private Sub Application_Exit(sender As Object, e As System.Windows.ExitEventArgs) Handles Me.Exit 'some logic here End Sub
  • Tyler Kendrick
    Tyler Kendrick almost 10 years
    It appears that the problem you are describing is an entirely different issue than the original problem described in this post. If you post a new question with the details about how/when you are invoking this event, I will be happy to help. Just post a link to the new question here, or send me a message with the link.
  • Xavier Peña
    Xavier Peña about 8 years
    FINALLY, this is the correct answer! I wish I could upvote you more than once. I've been searching for too long to get this right, and I finally find the right answer with zero upvotes at the bottom this hidden post ...
  • Xavier Peña
    Xavier Peña about 8 years
    ...although now I have encountered a different problem related to this solution. For those that might be interested: stackoverflow.com/q/35936891/831138
  • Catarina Ferreira
    Catarina Ferreira over 5 years
    Maybe it can work when binding is String, but doesn't work when is DateTime