Sunday, September 30, 2018

Styling the StringFormat - Summary

Continuing on from my previous blog on using a static resource to enforce a consistent stringformat across an application, I realized I had not covered the Infragistics SummaryDefinition. Infragistics has over-engineered the Summary Definition StringFormat and made our lives more difficult rather than easier.

The summary definition stringformat does not honor the simple string format I used ie: ###,###,##0.00;(###,###,##0.00). This is a problem and probably qualifies as a bug. If you read their documentation you can see they use a non-standard StringFormat. You can simplify their string format to {0:###,##0.00;(###,##0.00)} but no simpler. So we are forced to define a second format string specifically for summaries. The format required by columns is not compatible with the format required by the column's summary. This is bad design.

In the final project from my previous blog, after </igDP:FieldLayout.Fields> add


<igDP:FieldLayout.SummaryDefinitions>
    <igDP:SummaryDefinition SourceFieldName="Amount" Calculator="Sum" StringFormat="{}{0:###,##0.00;(###,##0.00)}" Position="UseSummaryPositionField"/>
</igDP:FieldLayout.SummaryDefinitions>

When you run the project you will see the XamDataGrid has a footer (you may need to increase the grid height).


Now we need to create a new static resource and use it. Add a static resource called SummaryDecimal to Dictionary1 and change the StringFormat to reference it. Note: I copied the StringFormat exactly from the XAML to the dictionary - this is wrong but do it anyway because it illustrates a common error.

Dictionary1.xaml:
<sys:String x:Key="SummaryDecimal">{}{0:###,##0.00;(###,##0.00)}</sys:String>


MainWindow.xaml
<igDP:SummaryDefinition SourceFieldName="Amount" Calculator="Sum" StringFormat="{StaticResource SummaryDecimal}" Position="UseSummaryPositionField"/>


If you run the project now something weird happens. The footer gets a prefix "Sum = " and the format is ignored. It took me a while to figure out what I did wrong.


In the StringFormat the leading {} is used to tell the XAML parser that the following { does not indicate the start of a markup extension - it 'escapes' the following { character, telling the parser to pass the string format (without the leading {}) to the StringFormat setter directly. This isn't needed when setting a static resource.

What I did above causes the leading {} to be sent to the StringFormat setter. It sees the leading {} and assumes I have not set a format so it uses the default format instead. I don't even think it parses anything beyond the leading {}. Infragistics, in their wisdom, has decided the default StringFormat for a summary should be <type> = <result>. 

Remove the leading {} from the static resource in Dictionary1.

<sys:String x:Key="SummaryDecimal">{0:###,##0.00;(###,##0.00)}</sys:String>

Now it works the way we expect.



No comments:

Post a Comment