如何将DateTime的默认值设置为空字符串?

时间:2022-06-22 22:53:42

I have a property called Raised_Time, this property shows the time at which alarm is raised in datagrid Cell. I don't want to show anything in the datagrid cell when user creates any alarm, it just display the empty cell.

我有一个属性叫做Raised_Time,这个属性显示了在datagrid单元格中触发警报的时间。我不想在datagrid单元格中显示任何内容当用户创建任何警报时,它只显示空单元格。

I googled in the internet and found that the default value of DateTime can be set using DateTime.MinValue and this will display MinValue of datetime i:e "1/1/0001 12:00:00 AM".

我在互联网上搜索,发现DateTime的默认值可以使用DateTime来设置。这将显示datetime i的最小值:e“1/1/0001 12:00:00 AM”。

Instead I want that the datagrid cell remain blank until alarm is raised, it don't show any time.

相反,我希望datagrid单元格保持为空,直到警报响起,它不会显示任何时间。

I think datatrigger can be written in this case. I am not able to write the datatrigger for this scenario. Do I need a converter also that checks if DateTime is set to DateTime.MinValue the leave the datagrid cell blank??

我认为在这种情况下可以编写datatrigger。我不能为这个场景编写数据处理程序。我是否需要一个转换器来检查DateTime是否被设置为DateTime。让datagrid单元格为空?

Please help!!

请帮助! !

4 个解决方案

#1


3  

How about just changing your property to link to a private field of DateTime e.g.:

把你的属性改成链接到DateTime的私有字段怎么样?

public string Raised_Time
{
  get
  {
    if(fieldRaisedTime == DateTime.MinValue)
    {
      return string.Empty();
    }
    return DateTime.ToString();
  }
  set
  {
    fieldRaisedTime = DateTime.Parse(value,   System.Globalization.CultureInfo.InvariantCulture);
  }
}

#2


8  

I would use a Converter for this because it's something I can easily see reusing in the future. Here's one I used to use that took a string value of the DateFormat as the ConverterParameter.

我会用转换器来做这个,因为我很容易看到它在未来被重用。这里有一个我曾经使用的,它将DateFormat的字符串值作为ConverterParameter。

public class DateTimeFormatConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if ((DateTime)value == DateTime.MinValue)
            return string.Empty;
        else
            return ((DateTime)value).ToString((string)parameter);
    }


    public object ConvertBack(object value, System.Type targetType, object parameter, CultureInfo culture)
    {
        throw new System.NotImplementedException();
    }
}

#3


7  

I see two easy options to solve this:

我看到了两个简单的解决办法:

  1. You use the Nullable data type DateTime?, so that you can store null instead of DateTime.MinValue if the alarm time is not set.

    您使用可空数据类型DateTime?,以便可以存储null而不是DateTime。如果没有设置警报时间,则最小化。

  2. You can use a converter, here is an example.

    您可以使用转换器,这里有一个例子。

#4


1  

I use a nullable datetime for this, with an extension method like:

为此,我使用了一个可空的datetime,并使用了如下扩展方法:

 public static string ToStringOrEmpty(this DateTime? dt, string format)
 {
     if (dt == null)
        return string.Empty;

     return dt.Value.ToString(format);
 }

#1


3  

How about just changing your property to link to a private field of DateTime e.g.:

把你的属性改成链接到DateTime的私有字段怎么样?

public string Raised_Time
{
  get
  {
    if(fieldRaisedTime == DateTime.MinValue)
    {
      return string.Empty();
    }
    return DateTime.ToString();
  }
  set
  {
    fieldRaisedTime = DateTime.Parse(value,   System.Globalization.CultureInfo.InvariantCulture);
  }
}

#2


8  

I would use a Converter for this because it's something I can easily see reusing in the future. Here's one I used to use that took a string value of the DateFormat as the ConverterParameter.

我会用转换器来做这个,因为我很容易看到它在未来被重用。这里有一个我曾经使用的,它将DateFormat的字符串值作为ConverterParameter。

public class DateTimeFormatConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if ((DateTime)value == DateTime.MinValue)
            return string.Empty;
        else
            return ((DateTime)value).ToString((string)parameter);
    }


    public object ConvertBack(object value, System.Type targetType, object parameter, CultureInfo culture)
    {
        throw new System.NotImplementedException();
    }
}

#3


7  

I see two easy options to solve this:

我看到了两个简单的解决办法:

  1. You use the Nullable data type DateTime?, so that you can store null instead of DateTime.MinValue if the alarm time is not set.

    您使用可空数据类型DateTime?,以便可以存储null而不是DateTime。如果没有设置警报时间,则最小化。

  2. You can use a converter, here is an example.

    您可以使用转换器,这里有一个例子。

#4


1  

I use a nullable datetime for this, with an extension method like:

为此,我使用了一个可空的datetime,并使用了如下扩展方法:

 public static string ToStringOrEmpty(this DateTime? dt, string format)
 {
     if (dt == null)
        return string.Empty;

     return dt.Value.ToString(format);
 }