Frage

In a DataGrid I have a column:

<DataGridTextColumn Header="Freeze First Day" Binding="{Binding FirstDay, StringFormat=\{0:d\}}"/>

and I want to be able to allow the fields to be changed from populated to blank. (FirstDay is of type DateTime.) However, as is, I get an error when I move away from editing that "Value '' could not be converted." I have tried a number of ways to fix this, but I am not seeing finding something that works.

War es hilfreich?

Lösung

Now, I think - I got root for your issue This is happening because of the StringFormat you had set. Whenever you delete the content the value becomes null. StringFormat cannot process NULL Values so it throws exception.

As an workaround, you can utilize Converter and there you can convert the object using ToString() Method and return if not NULL

//IN THE CONVERTER - YOU CAN RETURN AS,
{
     return (value!=null?value.ToString("{0:d}"):value);
} 

Andere Tipps

Consider to check if the Binding Property is of type nullable DateTime.

private DateTime? firstDay;
public DateTime? FirstDay
{
get
{ 
   return firstDay;
}
set
{
   firstDay=value;
}
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top