I've got a column in my data grid, which is data bound correctly and working. Now, I'd like to affect its format. So I've implemented a converter and connected it to the field.
<local:DateTimeFormat x:Key="IncludeTime" />
...
<igDP:Field Name="CreatedOn"
Label="Label"
Converter="{StaticResource IncludeTime}">
</igDP:Field>
Apparently, something is wrong, because I see no change in the window. However, when I put a break point in the converter, it gets hit and when I execute the statement in the Immediate Window, it gets the string as supposed to.
public class DateTimeFormat : IValueConverter
{
public object Convert(object v, Type t, object p, CultureInfo c)
{
if (v is DateTime)
return System.Convert.ToDateTime(v).ToString("yyyy-MM-dd HH:mm:ss");
return Binding.DoNothing;
}
...
}
My guess is that I'm not connecting the output of the converter to right piece of the mark-up but it beats me how to resolve it.
Please note that I'm looking for a general approach applying converters to fields in my data grid, so styling isn't going to do it (although there might be an approach covering dates, times and currencies).
I try to use 'ValueToDisplayTextConverters' for this scenario when I can.
xmlns:ie="http://infragistics.com/Editors"
...
<local:DateTimeFormat x:Key="IncludeTime" />
...
<igDP:Field Name="CreatedOn"
Label="Label">
<igDP:Field.Settings>
<igDP:FieldSettings>
<igDP:FieldSettings.EditorStyle>
<Style TargetType="{x:Type ie:XamDateTimeEditor}" BasedOn="{StaticResource {x:Type ie:XamDateTimeEditor}}">
<Setter Property="ValueToDisplayTextConverter" Value="{StaticResource IncludeTime}" />
</Style>
</igDP:FieldSettings.EditorStyle>
</igDP:FieldSettings>
</igDP:Field.Settings>
</igDP:Field>
Putting a converter on the Field is possible, though I'm not sure exactly why it's not working in your case. However, I find putting a converter on the Field to be useful when I have a more complex type/object than a DateTime bound to the field where I essentially want to do a ToString() to it.
It also changes the type of the value on the record's DataItem, which can cause issues later for sorting or exporting. For example, converting the data to a string in a Field converter means that sorting will be done on the string value rather than the DateTime. You can get around it by writing a custom SortComparer, but that's yet more code you have to write, which may not be necessary.