Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Problem handling right-to-left TextBox with percent character

I think there is a problem with WPF and right to left input when it comes to handling the percent sign (might also apply to similar signs).

When the TextBox is Right-to-Left oriented and there is no Hebrew characters at the start of the text, the percent sign is rendered on the wrong side of the number.

You can see the abnormal behaviour in this video:

Demonstration Screen Capture

Here is my application XAML (As you can see there is nothing in it):

<Window x:Class="HebrewTextTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow">
    <Grid FlowDirection="RightToLeft" Margin="10">
        <TextBox Text=" 25% הנחה"/>
    </Grid>
</Window>

Any idea if there is a solution for this?

Note that I do not have any control on the text, since the user types it in...

like image 803
Nir Kornfeld Avatar asked Mar 09 '20 09:03

Nir Kornfeld


1 Answers

On textbox text changed event add the right to left mark to the text:

<TextBox Name="textBox1" Text=" 25% הנחה" TextChanged="Textbox1_TextChanged"/>

bool addRtlMark = false;
private void Textbox1_TextChanged(object sender, TextChangedEventArgs e)
{
 if (!addRtlMark)
  {
    addRtlMark = true;
    textBox1.Text = "\u200f" + textBox1.Text.Replace("\u200f", "");
    addRtlMark  = false;
  }
}
like image 57
Miguel Carreira Avatar answered Oct 30 '22 08:10

Miguel Carreira