3

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

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438

3 Answers3

2

I came across the same problem when trying to format a string. I fixed it by setting the EditAsType property on the FieldSettings.

EditAsType = "{x:Type core:String}"

Of course, you can replace String with DateTime or other type.

The core namespace:

xmlns:core = "clr-namespace:System;assembly=mscorlib"
1

whatever format you want to see just create a style for XamDateTimeEditor and specify the format(mask) there. your code is right and returning the right value but the editor being used to display that value needs to be aware of it or it will just show value in default format. (or ValueToDisplayTextConverter can be used to display any type of text in any type of control.like alpha numeric text in numeric editor etc.)

And if you use a simple text editor you should be able to see the value returned by the converter.

<igEditors:XamDateTimeEditor Format="MM/dd/yy HH:mm:ss" Width="200" Height="30" Mask="mm/dd/yy hh:mm:ss" />
Kylo Ren
  • 8,551
  • 6
  • 41
  • 66
0

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.

Sean Beanland
  • 1,098
  • 1
  • 10
  • 25