开发者

DataGrid: Dynamic StringFormat

I'm facing a problem that I assumed would be very easy to solve but... Turns out it's not that easy.

Here is the situation: I created a custom UserControl which inherits from WPFToolkit's DataGrid (I'm required to work in .NET 3.5 :/ ).

This control is a matrix displaying financial values, and user should be able to choose the display format (percentage, absolute, percentage with 1 or 2 decimals...).

And... Maybe I'm just stupid here, but I can't solve it. My control has a custom DependencyProperty which contains a full market data referential, and then dispatches specific parts of the referential to specific properties (for example, the prices difference go to ItemsSource). Since users can change what is displayed(prices, price difference, yesterday's prices, other random financial stuff...), the display format will regularly change AND user should be allowed to select it itself.

My cells just follow a Style defined in my ResourceDictionary:

<Style x:Key="CellStyle" TargetType="{x:Type tk:DataGridCell}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type tk:DataGridCell}">
            开发者_开发技巧        <Grid Background="{TemplateBinding Background}">
                        <ContentPresenter
                            HorizontalAlignment="Center"
                                      VerticalAlignment="Center"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

I won't work with Binding's StringFormat cause I don't bind the ItemsSource (I set it in the code behind when the market referential property changed)

I already saw this answer, proposing a converter

However, seems like I don't know where should I add a converter to the cells...

Any ideas?

Thanks!


I finally solved this problem by adding a logical step in my Binding: now I set my ÌtemsSource to a string[] which is generated from the original double[], using ToString(CurrentFormat) when needed :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜