WPF formatting labels with StringFormat

后端 未结 2 2204
难免孤独
难免孤独 2021-02-14 10:04

I have a WPF application. I have some labels and some datagrids which are bound to some public properties. Some of these properties are numerical values.

In the datagrid

相关标签:
2条回答
  • 2021-02-14 10:22

    Just a quick addition I'd like to post along these lines in case anyone else runs in to it... My application uses localization since it has multi-country support, but we also support user's system settings. We noticed ContentStringFormat defaults to your UI culture.

    That caused an issue in one of our forms where the user's machine-specific decimal places they had configured in windows settings were not respected when you specified the ContentStringFormat.

    Since the ContentPresenter simply takes the string format without the converter culture you could normally specify in the binding , this means that a format of say: 0:N will only return two decimal places if English is my current UI culture, even though I have 5 decimals specified on my windows settings.

    In our case we applied some binding overrides to work around this, but mainly just wanted to add this extra bit of info in case anyone else runs in to it ;)

    0 讨论(0)
  • 2021-02-14 10:35

    For label you need to use ContentStringFormat:

    <Label Content="{Binding Obs.Tstat}" ContentStringFormat="{}{0:0.##}"/>
    

    Reason:

    Label's Content property is of type object and StringFormat is used only when binding property is of type String.

    If you try your code with TextBlock's Text property it will work fine with StringFormat because Text property is of type string.

    0 讨论(0)
提交回复
热议问题