0

I think this is a super basic question but I can't get it running. I want to show a fixed number inside my WPF View without a binding. This number is 0.001 or in german 0,001. See the seperator. Now if i switch the UIs language, the number seperator should be updated to the correct one of the language.

<TextBlock>
    <Run Text="0.001" />
    <Run Text=" " />
    ...
</TextBlock>

This should be extremly trivial and I think StringFormat should fit the needs, but as I said, i can't get it working. Thanks for your help

Solution: Thanks to @Corentin Pane pointing me to the solution. As he said, I need to declare the value

<TextBlock>
    <TextBlock.Resources>
        <system:Double x:Key="MinValue">0.001</system:Double>
    </TextBlock.Resources>

    <Run Text="{Binding Source={StaticResource MinValue}, Mode=OneTime, StringFormat='N3', ConverterCulture={x:Static gl:CultureInfo.CurrentCulture}}" />
    <Run Text=" " />
    ...
</TextBlock>
DirtyNative
  • 2,553
  • 2
  • 33
  • 58
  • First detail, `"0.001"` is a string, not a number; WPF has no idea, by default, that you're trying to display a number here. For potential solutions, see [this thread](https://stackoverflow.com/questions/520115/stringformat-localization-issues-in-wpf). – Kilazur Feb 05 '20 at 08:13
  • That's right. But this does not need to be a number, I could also make multiple and one containing only the "." or ",". But then I need to get the correct number seperator – DirtyNative Feb 05 '20 at 08:18

2 Answers2

1

If you want WPF to format your number appropriately, it should be a number from the start (like a double) and not a hardcoded string like "0.001". You can use a binding to a static resource:

<TextBlock>
    <TextBlock.Resources>
        <system:Double x:Key="myFixedValue">0.001</system:Double>
    </TextBlock.Resources>
    <TextBlock.Text>
        <Binding Source="{StaticResource myFixedValue}"/>
    </TextBlock.Text>
</TextBlock>

with the following namespace:

xmlns:system="clr-namespace:System;assembly=mscorlib"

Now you can worry about the formatting, and as stated in a comment, this thread provides some hints. For example you can change your Binding to:

<Binding Source="{StaticResource myFixedValue}"
         StringFormat="f"
         ConverterCulture="{x:Static gl:CultureInfo.CurrentCulture}"/>

and add

xmlns:gl="clr-namespace:System.Globalization;assembly=mscorlib"

declaration.

Corentin Pane
  • 4,794
  • 1
  • 12
  • 29
0

I had a real-world example of this that outputs "XXXXX from $0.00 to YYYY" as a tool tip. This is a tad more complex because it shows leading and trailing text and use of a converter with the static/constant number.

Background:

  • Properties holds the current currency symbol.
  • CurrencyConverter is a converter that formats the double so it looks like "$0.00".
  • ZERO is my constant value.

Code:

<TextBlock>
  <TextBlock.Resources>
     <system:Double x:Key="ZERO">0.00</system:Double>
  </TextBlock.Resources>
  XXXXX from 
  <Run>                                                    
    <Run.Text>
      <MultiBinding Converter="{StaticResource CurrencyConverter}">
        <Binding Source="{StaticResource ZERO}" Mode="OneWay"/>
        <Binding Source="{x:Static properties:Settings.Default}" Path="CurrencySymbols" Mode="OneWay" />
      </MultiBinding>
    </Run.Text>
  </Run>
  to YYYY 
</TextBlock>

Overall, I recommend that the accepted answer could be slightly improved by adding the additional Mode="OneWay" to the binding since it is a static number.

Eric Wood
  • 331
  • 1
  • 3
  • 14