C#和日期选择器,怎么改格式? [英] c# and date picker, how to change format?

查看:130
本文介绍了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.

在我的日期选择器的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>

这似乎是第一次,所有工作正常,我可以看到的格式的日期,我想,我可以手动或使用日历,并以两种方式更改日期的到达视图模型是正确的日期。

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?

感谢。
Daimroc。

Thanks. Daimroc.

推荐答案

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

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>



最后,在视图模型,该属性必须是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);
            }
        }
    }



我希望这会帮助你

I hope this will help you

问候

克劳德

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

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