tags:

views:

49

answers:

1

I've got a WPF DataGrid and wish to apply a currency format to several of the columns. I can do this on a per-column basis using Binding={Binding FieldName, StringFormat={}{0:C}, but I'd prefer to define the currency format once - presumably in a <Style> resource - and just apply that resource to each column that needs formatting.

Problem is, I've no idea how to set a Binding's StringFormat property from within a style. Is it possible to set that or a similar property in a <Style>, or am I barking up the wrong tree with this approach?

Update: I've successfully applied a style to the cells in my DataGridTextColumn, and setting the ContentStringFormat property of the cell seems like it'd be the way to go... but whatever value I put in there seems to get ignored.

+1  A: 

You could create a Template for a DataGrid cell, and use the {TemplateBinding ContentPresenter.Content} value for the converter parameter.

I haven't tested this code, but something like:

<ControlTemplate TargetType="{x:Type DataGridCell}" x:Key="CurrencyFormatCell">
    <TextBlock 
        Text="{TemplateBinding ContentPresenter.Content, Converter={StaticResource FormatCurrencyConverter}}"
        ForeGround="{TemplateBinding ContentPresenter.Content, Converter={StaticResource CurrencyTextColorConverter}}"
        />
</ControlTemplate>
Rachel
Sorry this took so long to accept - it's taken me a while to come back around to this. In order to encapsulate everything that's been mentioned I probably *would* end up going with a cell template, rather than a style. Thanks!
djacobson