阿拉伯语的 WPF 文本框流向问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17472469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:09:00  来源:igfitidea点击:

WPF Textbox Flow direction issue with Arabic language

wpfwinformsarabic

提问by Sumit Bakshi

I am working on an application which supports many languages including Arabic. I am observing an odd behavior in the WPF text box while entering the values but strangely the same is working fine in a Windows Form text box.

我正在开发一个支持包括阿拉伯语在内的多种语言的应用程序。在输入值时,我在 WPF 文本框中观察到一个奇怪的行为,但奇怪的是,在 Windows 窗体文本框中它也能正常工作。

All I am doing is this

我正在做的就是这个

  1. Create a Text Box and keep it's flow direction to the default value which is LeftToRight
  2. Change the language of the app to Arabic.
  3. Type the following letter one by one in the text box 1 a 2 j

    (where a and j denotes any two Arabic character).

  1. 创建一个文本框并将其流向保持为默认值 LeftToRight
  2. 将应用程序的语言更改为阿拉伯语。
  3. 在文本框中一一输入以下字母 1 a 2 j

    (其中 a 和 j 表示任意两个阿拉伯字符)。

Result : Wpf Text box shows the value as 1j2a While the Win forms text box displays it as j2a1.

结果:Wpf 文本框将值显示为 1j2a 而 Win 表单文本框将其显示为 j2a1。

Since Arabic is right to left , so the value shown by Win form is correct but that of WPF is wrong.

由于阿拉伯语是从右到左,所以Win形式显示的值是正确的,而WPF的值是错误的。

Wpf displays the correct value if we type the first character as Arabic and then enter any non-Arabic character.

如果我们将第一个字符输入为阿拉伯语,然后输入任何非阿拉伯字符,Wpf 会显示正确的值。

Also, if I set the FlowDirection of the TextBox to RightToLeft, then WPF textbox works fine. But that will make my string move to the right and the app will have alignment issues.

此外,如果我将 TextBox 的 FlowDirection 设置为 RightToLeft,则 WPF 文本框工作正常。但这将使我的字符串向右移动,应用程序将出现对齐问题。

Does anyone have any idea on why WPF text box shows the incorrect value while windows forms works fine ?

有没有人知道为什么 WPF 文本框显示不正确的值而 Windows 窗体工作正常?

Also, can we keep the text aligned to left if we set the flow direction as RightToLeft ?

另外,如果我们将流向设置为 RightToLeft ,我们可以保持文本向左对齐吗?

采纳答案by Viv

Also, if I set the FlowDirection of the TextBox to RightToLeft, then WPF textbox works fine. But that will make my string move to the right and the app will have alignment issues.

此外,如果我将 TextBox 的 FlowDirection 设置为 RightToLeft,则 WPF 文本框工作正常。但这将使我的字符串向右移动,应用程序将出现对齐问题。

That kinda says you're having bigger problems than basic alignment. The whole point of FlowDirectionis to allow those non native LeftToRightreaders to have a native UX in your application.

这有点说你有比基本对齐更大的问题。重点FlowDirection是允许那些非本地LeftToRight读者在您的应用程序中拥有本地用户体验。

string moving to the right is what it should be and that's what your Arabic users would expect.

向右移动的字符串应该是这样,这就是您的阿拉伯语用户所期望的。

Microsoft Home Page in en-US

位于美国的 Microsoft 主页

Same page in ar-iq

ar-iq 中的同一页面

There is a reason why not just characters look different, but the actual text-flow as well(It's what users of that language would expect)

有一个原因不仅是字符看起来不同,还有实际的文本流(这是该语言的用户所期望的)

If you're sure you want to keep the content aligned left even when FlowDirectionis reversed, apply a Style.Triggerto do so accordingly but do look into how your UX feels.

如果您确定即使在FlowDirection反转时也要保持内容左对齐,请相应地应用 aStyle.Trigger这样做,但请注意您的 UX 感觉。

<TextBox>
  <TextBox.Style>
    <Style TargetType="{x:Type TextBox}">
      <Setter Property="HorizontalContentAlignment"
              Value="Left" />
      <Style.Triggers>
        <Trigger Property="FlowDirection"
                  Value="RightToLeft">
          <!-- "Right" - HorizontalContentAlignment with FlowDirection="RightToLeft" is
               the same end render visually as "Left" - HorizontalContentAlignment with
               FlowDirection="LeftToRight" -->
          <Setter Property="HorizontalContentAlignment"
                  Value="Right" />
        </Trigger>
      </Style.Triggers>
    </Style>
  </TextBox.Style>
</TextBox>

Update:

更新:

As the OP now states the control's actually a TextBlockthe above Stylewould look like:

正如 OP 现在所说,控件实际上是一个TextBlock上面的Style样子:

<TextBlock>
  <TextBlock.Style>
    <Style TargetType="{x:Type TextBlock}">
      <Setter Property="TextAlignment"
              Value="Left" />
      <Style.Triggers>
        <Trigger Property="FlowDirection"
                  Value="RightToLeft">
          <Setter Property="TextAlignment"
                  Value="Right" />
        </Trigger>
      </Style.Triggers>
    </Style>
  </TextBlock.Style>
</TextBlock>

回答by Vishal

What if u make textbox flowdirection="RightToLeft"

如果你让文本框 flowdirection="RightToLeft"

in Xaml

在 Xml 中

<TextBox Name="TxtBox1" FlowDirection="RightToLeft">