Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Format Percentage

Format Percentage

Scheduled Pinned Locked Moved WPF
databasewpfwcfhelp
3 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I have a nullable decimal TaxRate property on an entity. I want to format/style the textbox to enter a percentage. What I have so far doesn't work. If I start out by entering a decimal point, then it works fine. If I start out by entering a digit, then I cannot enter a decimal.

    <Setter Property="Text" Value="{Binding SelectedStatesCountiesCities.TaxRate, StringFormat={}{0:N2}%}" />
    <Style.Triggers>
        <Trigger Property="IsKeyboardFocused" Value="True">
            <Setter Property="Text" Value="{Binding SelectedStatesCountiesCities.TaxRate}" />
        </Trigger>
    </Style.Triggers>
    

    If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

    Richard DeemingR 1 Reply Last reply
    0
    • K Kevin Marois

      I have a nullable decimal TaxRate property on an entity. I want to format/style the textbox to enter a percentage. What I have so far doesn't work. If I start out by entering a decimal point, then it works fine. If I start out by entering a digit, then I cannot enter a decimal.

      <Setter Property="Text" Value="{Binding SelectedStatesCountiesCities.TaxRate, StringFormat={}{0:N2}%}" />
      <Style.Triggers>
          <Trigger Property="IsKeyboardFocused" Value="True">
              <Setter Property="Text" Value="{Binding SelectedStatesCountiesCities.TaxRate}" />
          </Trigger>
      </Style.Triggers>
      

      If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      The UpdateSourceTrigger=PropertyChanged option means that your source property is updated every time you press a key in the TextBox. When you type a . character, one of two things will happen:

      • The box is currently empty. "." on its own cannot be parsed as a decimal, so the property will not be updated.
      • The box contains an integer:
        • decimal.Parse("42.") returns 42.
        • The property is updated to 42, triggering the PropertyChanged event.
        • The binding on the box updates to reflect the new property value - the text is set to "42".

      Depending on what you are trying to do, there may be ways around this - for example, manually updating the source value when the user presses Enter: c# - Binding to a double with StringFormat on a TextBox - Stack Overflow[^]


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      K 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        The UpdateSourceTrigger=PropertyChanged option means that your source property is updated every time you press a key in the TextBox. When you type a . character, one of two things will happen:

        • The box is currently empty. "." on its own cannot be parsed as a decimal, so the property will not be updated.
        • The box contains an integer:
          • decimal.Parse("42.") returns 42.
          • The property is updated to 42, triggering the PropertyChanged event.
          • The binding on the box updates to reflect the new property value - the text is set to "42".

        Depending on what you are trying to do, there may be ways around this - for example, manually updating the source value when the user presses Enter: c# - Binding to a double with StringFormat on a TextBox - Stack Overflow[^]


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        Thanks for the reply. Setting UpdateSourceTrigger to LostFocus dit it BTW, here's an interesting solution: [c# - How do I get a TextBox to only accept numeric input in WPF? - Stack Overflow](https://stackoverflow.com/questions/1268552/how-do-i-get-a-textbox-to-only-accept-numeric-input-in-wpf)

        If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups