Home > Software design >  How to format text strings as numbers in a TextBox?
How to format text strings as numbers in a TextBox?

Time:11-09

I have a textbox bound to a string property, containing numeric values

<TextBox Name="txtInvAmount" Text="{Binding Path=InvAmount,UpdateSourceTrigger=PropertyChanged, StringFormat=\{0:F\}}"/>


Private _invAmount as String
Public Property InvAmount As String
    Get
                _invAmount = _objInvoice.AMOUNT.ToString
        Return _invAmount 
    End Get
    Set(ByVal value As String)
        _invAmount = value
        If IsNumeric(_invAmount) Then
            _objInvoice.AMOUNT = value
        Else
            _objInvoice.AMOUNT = Nothing
        End If
        NotifyPropertyChange("InvAmount")
    End Set
End Property

how can I get my string to be formatted as a number, using "." as thousands separator and "," as decimal separator (eg: "1000" should be formatted as "1.000,00")?

PS: I also tried to set

Public Property InvAmount As Double?

that made text value to be displayed with trailing zeros, but not with thousands separator (ie: "1000" is displayed as "1000,00", but not as "1.000,00")

CodePudding user response:

Why don't you make the InvAmount property type Decimal?
Then you can specify the desired format in the binding in the StringFormat property.

Example:

<StackPanel>
    <FrameworkElement.Resources>
        <sys:Decimal x:Key="numeric">123456789.01</sys:Decimal>
    </FrameworkElement.Resources>
    <TextBlock Text="{Binding Source={StaticResource numeric},
                              StringFormat='0,0.00'}"/>
</StackPanel>

Decimal and place separator signs vary by culture. Culture in WPF is set by Language. The default is "en-us". But you can explicitly set it in any element. It will be applied to it and to its children.
You can also explicitly set the culture in the binding.

<StackPanel Language="de">
    <FrameworkElement.Resources>
        <sys:Decimal x:Key="numeric">123456789.01</sys:Decimal>
    </FrameworkElement.Resources>
    <TextBlock Text="{Binding Source={StaticResource numeric},
                              StringFormat='0,0.00'}"/>
    <TextBlock Text="{Binding Source={StaticResource numeric},
                              StringFormat='0,0.00',
                              ConverterCulture=ru-ru}"/>
</StackPanel>

CodePudding user response:

I converted InvAmount property to Nullable(Of Decimal), and placed resource into my usercontrol

<UserControl x:Name="InvoiceControl"
  ...
  xmlns:sys="clr-namespace:System;assembly=mscorlib"
 >
 <UserControl.Resources>
     <sys:Decimal x:Key="numeric">123456789.01</sys:Decimal>
 </UserControl.Resources>
    <Grid Name="grdInvoice">
       <TextBox Name="txtInvAmount" Text="{Binding Path=InvAmount,UpdateSourceTrigger=LostFocus, Source={StaticResource numeric}, StringFormat='0.0,00'}"/>
   </Grid> 
</UserControl>

Property value is not displayed. If I skip StaticResource from textbox, InvAmount value is displayed. I also tried to move StaticResource definition into <Grid.Resources>, but InvAmount is not being displayed into textBox

  • Related