Custom cell formatting

Sep 5, 2008 at 6:08 PM
Edited Sep 5, 2008 at 8:43 PM
How may I supply a custom format provider for cells of a given column in the DataGrid?

Related question: I have found DataFieldBindng.StringFormat is used to apply formatting to cell values (for types that implement IFormattable apparently).  Will this be the the format supplied to a custom formatter?

Thank you very much.
Sep 8, 2008 at 5:22 PM
Binding.StringFormat will be the format used on the binding.  When you say "Will this be the the format supplied to a custom formatter?", is the custom formatter like a converter that you are using?

For more info in StringFormat you can look here,
Sep 9, 2008 at 10:36 PM
Edited Sep 9, 2008 at 11:32 PM
Actually, I'm not refering to a value converter.  I'm referring to a type that implements IFormatProvider which provides an object of type ICustomFormatter (usually the same instance).

For example, I have a column (in a winforms) DataGridView bound to a TimeSpan type object.  Since TimeSpan doesn't implement IFormattable itself, I supply a custom format provider and formatter as explained here:

How can I supply this same formatter to the new WPF grid so that I don't have to create a value converter and can use what I've already coded up?  I guess I'm expecting your wpf 'DataGridBoundColumn.DataFieldBinding' to have the same capabilities as the current System.Windows.Forms.Binding in that you can give it a FormatProvider as well as specify the FormatString.

(I should clarify myself regarding the Binding type because there are known defects that cause it to not always apply the formatting of the format provider, but we work around this by extending the Binding class and applying the custom formatting in the 'OnFormat' override.)

And thanks for the link to new StringFormat info.  Cool new feature.  Will this work similarly in the 'DataGridBoundColumn.DataFieldBinding'?
Sep 10, 2008 at 6:27 PM
With the current implementation of BindingBase you will have to use a value converter to supply your custom format. 

DataGridBoundColumn.DataFieldBinding is of type BindingBase so it will work.
Sep 12, 2008 at 12:22 AM
Edited Sep 12, 2008 at 8:29 PM
Ok, thanks for your help.

It feels like the MS direction with WPF is to move from FormatProviders and CustomFormatters to ValueConverters that convert to and from a custom type (or TimeSpan for example) to a string type.  I think its a good direction as value converters feel more intuitive and straightforward, and its a model that applies to converting to other types besides strings.

So to confirm, if have a value converter that also did custom formatting based on the object parameter available on the convert methods, will the value in Binding.StringFormat you mentioned be passed in so I can customize presentation formats with the same converter, and declaratively in XAML?

Sep 19, 2008 at 5:13 PM

The value in Binding.StringFormat is not passed in to the Convert (or ConvertBack) method of a value converter.


When a binding has both a converter and a StringFormat, the source value is first sent through the converter:

                value = converter.Convert(value, targetPropertyType, binding.ConverterParameter, culture)

Then if the target property expects a string, the result is passed through String.Format:

                value = String.Format(culture, binding.StringFormat, value)


If you want to pass some kind of format string into a converter, use the ConverterParameter.