0

I want to add percent sign "%" to any input text in wpf TextBox when the user insert a text.

So when the user will enter a number, will be added % sign to any number in the input box, for example: 5 will shown at the Text box as 5% 0 - 0% 100 - 100%

I tried the following code:

<TextBox x:Name="TextBoxInputValue" Text="{Binding AddPercentSign, UpdateSourceTrigger=PropertyChanged, StringFormat={}{0}%}" Style="{StaticResource @TextBoxStyle}" Width="100" Height="20"></TextBox>

and:

public int AddPercentSign{ get; set; }

and:

TextBoxInputValue.DataContext = this;

But it has no effect on the TextBox when the user insert an input...

How can I achieve that result?

Thank you

4 Answers 4

1

You could use a flag that decides whether you should actually set the Text property in the event handler:

private bool _handleEvent = true;
private void TextChanged(object sender, TextChangedEventArgs e)
{
    if (_handleEvent)
    {
        _handleEvent = false;
        MyTextBox.Text = MyTextBox.Text + "%$#";
        _handleEvent = true;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks this is working... But not quiet as I exepted... I have edited my question
Can't you just change the code to: MyTextBox.Text = MyTextBox.Text + "%"; ?
No cause then when the user insert for example 12345 I get: 12345%%%%%
Try to bind to an int/double/decimal property use a StringFormat as suggested by @Mike Eason then. And please don't change your original question completely once it has been answered. Ask a new question if you have another issue.
0

If you are binding your Text property, you can use StringFormat.

<TextBox Text="{Binding SomeProperty, StringFormat={}{0}%}" />

Check out this tutorial.

1 Comment

I tried adding: "{Binding Path=Percentage, StringFormat={}{0}%}" as in this question: stackoverflow.com/questions/1283049/… but it is doing nothing to my TextBox input... I have edited my question
0

But it has no effect on the TextBox when the user insert an input...

You need to define the source property and set the DataContext of the TextBox to the class where it is defined, e.g.:

<TextBox x:Name="textBox1" Text="{Binding SomeProperty, UpdateSourceTrigger=PropertyChanged, StringFormat='{}{0}%'}" />

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        textBox1.DataContext = this;
    }

    public int SomeProperty { get; set; }
}

5 Comments

Did you really add the AddPercentSign property to the same class where the TextBoxInputValue is defined? If you don't see a value in the TextBox the binding doesn't work.
Yes I have added this: public int AddPercentSign{ get; set; } to the same class
Could you please post your entire code-behind file?
It's a huge file... from the other side can you share an example of working code that does this job? of adding % sign to any text insert into TextBox
I already did. It is just that you cannot seem to apply it to your code. That's why I need to see your code it in order to be able to tell you what's wrong with it.
0

Checkout this link, even I was facing somewhat similar problem Solution in this link can me modified according to your condition.

Add percent sign "%" to any input text in wpf TextBox when the user insert a text

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.