c# 和日期选择器,如何更改格式? [英] c# and date picker, how to change format?

查看:18
本文介绍了c# 和日期选择器,如何更改格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 C# 4.0 (WPF) 应用程序中有一个日期选择器,我想将文本框中可见的日期格式更改为 yyyy/MM/dd.现在我看到格式为 dd/MM/yyyy.

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.

在我的 datePicker 的 axml 中,我有这个代码:

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>

这似乎是第一次一切正常,我可以看到我想要的格式的日期,我可以手动或使用日历更改日期,并且在这两种方式中到达 viewModel 的日期都是正确的.

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?

谢谢.戴姆洛克.

推荐答案

您可以尝试以下解决方案.

you can try the following solution.

首先创建以下转换器:

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

然后在 xaml 中,您必须创建转换器的实例并在 DatePicker 的文本框中使用它

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>

最后,在viewmodel中,属性必须是DateTime类型吗?(即一个可为空的 DateTime).

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

希望对你有帮助

问候

克劳德

这篇关于c# 和日期选择器,如何更改格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆