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,261
  • 2
  • 23
  • 48
  • 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

1 Answers1

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,680
  • 1
  • 13
  • 29