views:

47

answers:

4

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

A: 

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

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

     return dt.Value.ToString(format);
 }
fearofawhackplanet
+1  A: 

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);
  }
}
ChrisBD
+2  A: 

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.

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

Heinzi
+1  A: 

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.

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