Search code examples
wpfdatetimedatagriddatatrigger

How to set the default value of DateTime to empty string?


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.

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".

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

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??

Please help!!


Solution

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

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