c# and date picker, how to change format?

2019-02-19 09:06发布

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.

2条回答
forever°为你锁心
2楼-- · 2019-02-19 09:34

defaultly the DateTimerPicker does not support null values.

Maybe this post from MSDN with the same topic can help you.

There you will find other ideas how to implement it or some code project for nullable date time picker.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-02-19 09:36

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

查看更多
登录 后发表回答