Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

wpf - c# and date picker, how to change format?

I have a datepicker in my C# 4.0 (WPF) application and I would like to change the format of the date that is visible in the textBox to yyyy/MM/dd. Now I see the format dd/MM/yyyy.

In my axml of the datePicker I have this code:

<DatePicker Height="25" HorizontalAlignment="Left" Margin="5,36,0,0" Name="dtpStartDate"
                    SelectedDate="{Binding StartDateSelectedDate}" 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>

This seems in a first time that all works fine, I can see the date in the format that I want, and I can change the date manually or using the calendar, and in both ways the date that arrives to the viewModel is the correct.

But I have a problem, because I would like to detect that if the date is empty, in my view model control this case. But If I clear the datepicker, in my view model arrives the last correct date, so I can't check if the date is empty or not.

So how can I modify the format of the date in the date picker and control if the date is empty/null or not?

Thanks. Daimroc.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can try the following solution.

First create the following converter :

public class StringToDateTimeConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
        {
            return null;
        }
        return ((DateTime)value).ToString(parameter as string, CultureInfo.InvariantCulture);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (string.IsNullOrEmpty(value as string))
        {
            return null;
        }
        try
        {
            DateTime dt = DateTime.ParseExact(value as string, parameter as string, CultureInfo.InvariantCulture);
            return dt as DateTime?;
        }
        catch (Exception)
        {
            return null;
        }
    }
}

Then in the xaml, you will have to create an instance of the converter and use it in the textbox of the DatePicker

<Window x:Class="TestDatePicker.MainWindow"
    ... 
    xmlns:converters="clr-namespace:TestDatePicker"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    ...
    <converters:StringToDateTimeConverter x:Key="StringToDateTimeConverter" />
</Window.Resources>
<Grid DataContext="{StaticResource MainWindowVM}">
    ...
    <DatePicker Height="25" HorizontalAlignment="Left" Margin="5,36,0,0" Name="dtpStartDate"
                SelectedDate="{Binding StartDateSelectedDate}" 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}}, Converter={StaticResource StringToDateTimeConverter}, ConverterParameter=yyyy/MM/dd}" />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DatePicker.Resources>
    </DatePicker>
    ...
</Grid>

Finally, in the viewmodel, the property must be of type DateTime? (i.e a nullable DateTime).

    private DateTime? _startDateSelectedDate;
    public DateTime? StartDateSelectedDate
    {
        get { return _startDateSelectedDate; }
        set
        {
            if (_startDateSelectedDate != value)
            {
                _startDateSelectedDate = value;
                RaisePropertyChanged(() => this.StartDateSelectedDate);
            }
        }
    }

I hope this will help you

Regards

Claude


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...