Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DatePicker format

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.

like image 849
Indu Avatar asked Jun 25 '14 17:06

Indu


3 Answers

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.

like image 96
Dave Williams Avatar answered Sep 19 '22 13:09

Dave Williams


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.

like image 39
Tyler Kendrick Avatar answered Sep 18 '22 13:09

Tyler Kendrick


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)
    {
    }
}
like image 44
user3776442 Avatar answered Sep 20 '22 13:09

user3776442