3

I can't write dot('.') or zero after dot(.0) in a textbox that is bounded to a float property while UpdateSourceTrigger is set to PropertyChanged.I know this is because binding to a float data type will automatically cause WPF to add a float validator.I should use a different DataAnnotation for my float property or write my own validator.but i dont know how can i do this?any idea?

mahboub_mo
  • 2,908
  • 6
  • 32
  • 72

2 Answers2

1

This is a common problem. You can fix this issue by simply adding a StringFormat to your Binding... If you wanted to allow 2 decimal places, you could use this:

<TextBox Text="{Binding Value, StringFormat={}{##.##}, FallbackValue=0.00}" />

For three decimal places, you could use this:

<TextBox Text="{Binding Value, StringFormat={}{##.###}, FallbackValue=0.00}" />

And so on.


UPDATE >>>

This is crazy... I'm sure that was what I used before but it doesn't seem to work now. I can't seem to find my original example, but after experimenting I have found the following formats that work to some degree:

<TextBox Text="{Binding Value, StringFormat=N, FallbackValue=0.00}" />

<TextBox Text="{Binding Value, StringFormat=N2, FallbackValue=0.00}" />

<TextBox Text="{Binding Value, StringFormat=N3, FallbackValue=0.00}" />

I'm still looking for my original example now.

UPDATE 2 >>>

Unfortunately, I can't find my original example... if only I could remember which application it was in. However, after a quick search online, I found a similar post (Weird TextBox issues with .NET 4.5 - no '.' allowed) that I answered a while ago and that post had the same answer which seemed to work for that user.

I totally accept that this is not working now (for either of us) and I can't work out why, so I can only off you the three formats provided in my first update.

Community
  • 1
  • 1
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • Thanks.but after adding this `StringFormat`,although the binding property has value,the text value is empty while data is loading from model!do you know why? – mahboub_mo Oct 23 '13 at 11:41
  • Just add the `FallbackValue` property to the `Binding` like in my edited example. – Sheridan Oct 23 '13 at 11:45
  • 1
    Now it shows `0.00` instead of the right value!have you tried it yourself? – mahboub_mo Oct 23 '13 at 11:55
  • The property has value,but this stringformat prevent showing that in the textbox. – mahboub_mo Oct 23 '13 at 12:03
  • 1
    Yeah, I've just seen your problem... I could have sworn that that was the fix I used before. I'm just trying to find the fix in my code but I can't remember which application it was in... I'll get back to you. For now, you can use `StringFormat=N3` or `StringFormat=N2` but there *is* a better format to use. – Sheridan Oct 23 '13 at 12:09
  • Thank you so much.Yeah i know.I found a similar answer here:http://stackoverflow.com/questions/14600842/wpf-bind-textbox-to-float-value-unable-to-input-dot-comma! .But i don't know why its not working for me!Thanks again.It'm waiting.. – mahboub_mo Oct 23 '13 at 12:11
  • 1
    just for knowing,I just realized the problem is happening in `.netFramerwork4.5`.There no similar problem in earlier versions! – mahboub_mo Oct 24 '13 at 08:49
  • 1
    +1 That's interesting... thanks for coming back and sharing that. – Sheridan Oct 24 '13 at 08:51
1

Adding StringFormat=N2 would create some strange and unwanted behavior

The simplest solution for that is to change UpdateSourceTrigger=PropertyChanged to LostFocus

But if you really want to use PropertyChanged, it is possible to use extra property and bind to it like follows: (in this case I am interested to ValueAddedTax but I bind to ValueAddedTaxString)

    private float valueAddedTax;
    [Display(Name = "Value Added Tax")]
    [Range(0, 100)]
    public float ValueAddedTax
    {
        get { return valueAddedTax; }
        set
        {
            SetProperty(ref valueAddedTax, value);
            valueAddedTaxString = value.ToString();
        }
    }

    private string valueAddedTaxString;
    [NotMapped]
    public string ValueAddedTaxString
    {
        get { return valueAddedTaxString; }
        set
        {
            valueAddedTaxString = value;
            if (!value.EndsWith("."))
            {
                float temp;
                if (float.TryParse(value, out temp))
                    ValueAddedTax = temp;
            }
        }
    }

If you remove "if (!value.EndsWith("."))" It behave exactly the same as the default behavior!!

mesmoll
  • 167
  • 2
  • 7