在 WPF 应用程序中绑定可为空的日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28561190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:57:34  来源:igfitidea点击:

Binding a nullable datetime within WPF application

c#.netwpfdatetimebinding

提问by Lamloumi Afif

I have a wpf application in which I had this property to bind to a datepicker

我有一个 wpf 应用程序,在其中我将此属性绑定到日期选择器

public Nullable<System.DateTime> dpc_date_engagement { get; set; }

So I add a converter :

所以我添加了一个转换器:

 public class DateConverter : IValueConverter
   {
       public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value != null)
            return ((DateTime)value).ToShortDateString();
        return String.Empty;
    }

       public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
       {
           string strValue = value.ToString();
           DateTime resultDateTime;
           return DateTime.TryParse(strValue, out resultDateTime) ? resultDateTime : value;
       }
   }

In XAML file :

在 XAML 文件中:

                     <DatePicker >
                                <DatePicker.Text>
                                    <Binding Path="dpc_date_engagement" UpdateSourceTrigger="PropertyChanged">
                                        <Binding.Converter>
                                            <converter:DateConverter/>
                                        </Binding.Converter>
                                    </Binding>
                                </DatePicker.Text>
                            </DatePicker>

The problem is when the date is null, the displayed text is 1/1/0001.

问题是当日期为空时,显示的文本是 1/1/0001。

  • How can I fix my code to display an empty string instead of 01/01/0001, for null values?
  • 对于空值,如何修复我的代码以显示空字符串而不是 01/01/0001?

回答by Clemens

The Nullable valuepassed to your converter is not itself null, even if it holds a nullvalue (it can't even be null, because it is a struct and therefore not nullable).

value传递给转换器的 Nullable不是它自己null,即使它持有一个null值(它甚至不能为空,因为它是一个结构体,因此不可为空)。

So instead of comparing valueto null, you'll have to cast it to Nullable<Datetime>and then check its HasValueproperty.

因此,而不是比较valuenull,你就必须将它转换为Nullable<Datetime>,然后检查其HasValue属性。

Moreover, you seem to have something like DateTime.MinValuein your bound property instead of null. So you should check against that, too:

此外,您DateTime.MinValue的绑定属性中似乎有类似的东西,而不是null. 所以你也应该检查一下:

public object Convert(...)
{
    var nullable = (Nullable<DateTime>)value;

    if (nullable.HasValue && nullable.Value > DateTime.MinValue)
    {
        return nullable.Value.ToShortDateString();
    }

    return String.Empty;
}

回答by Jorge Villalobos Carvajal

The easiest way I've been found to handle nullableDateTimefield using in a DatePickeris setting TargetNullValue=''

我发现在DatePicker 中使用处理空的DateTime字段的最简单方法是设置TargetNullValue=''

In XAML file:

在 XAML 文件中:

<DatePicker Text={Binding dpc_date_engagement, Mode=TwoWay, TargetNullValue='', UpdateSourceTrigger=PropertyChanged} />