다음을 통해 공유


WPF ignoring BiDi control characters

Hello everyone,

Have you ever tried using the following BiDirectional (Disclaimer: The content linked here is not from Microsoft: https://en.wikipedia.org/wiki/Bi-directional_text https://unicode.org/reports/tr9/) control characters in WPF?

  • LRM (U+200E, Left-To-Right Mark)
  • LRE (U+202A, Left-To-Right Embedding)
  • LRO (U+202D, Left-To-Right Override)
  • PDF (U+202C, Pop Directional Formatting)

If the answer is yes, you will have noticed that WPF is completely ignoring them. (That is: It is ignoring all except for the LRM mark, which only works when the Language is set to "en-US"!)
Silverlight, on the other hand, does regard them and renders the affected text correctly.

Here is a comparison, first in WPF and then in Silverlight:

Windows Presentation Foundation Silverlight

 

I used exactly the same code for both examples. (Please note that I replaced the real BiDi control characters with bold text (e.g. [LRM] ), to avoid confusion and compatibility issues.)

 <StackPanel FlowDirection="RightToLeft" Language="fa-IR">     <StackPanel Margin="10">         <TextBlock  FlowDirection="LeftToRight">LRM</TextBlock>         <TextBlock> [LRM] 0  [LRM] 1  [LRM] 2  [LRM] 3  [LRM] 4  [LRM] 5  [LRM] 6  [LRM] 7  [LRM] 8  [LRM] 9</TextBlock>     </StackPanel>     <StackPanel Margin="10">         <TextBlock FlowDirection="LeftToRight">LRE & PDF</TextBlock>         <TextBlock> [arabic text][LRE] ?0 1 2 3 4 5 6 7 8 9 [ PDF][arabic text] </TextBlock>     </StackPanel>     <StackPanel Margin="10">         <TextBlock FlowDirection="LeftToRight">LRO</TextBlock>         <TextBlock> [LRO] 0 1 2 3 4 5 6 7 8 9</TextBlock>     </StackPanel> </StackPanel> 

 

Note how WPF does change the way it treats the LRM character when you set the Language attribute to en-US:

Windows Presentation Foundation

 

However curious this may seem: This is actually not a bug, but desired behavior!

The reason for the difference is that WPF offers a different, higher-level way of implementing bidirectional text, to enable the implementation of RichText controls. (See here for a detailed guide on BiDi in WPF: https://msdn.microsoft.com/en-us/library/aa350685.aspx

The correct implementation (in WPF) for the example above would look like this:

 <StackPanel FlowDirection="RightToLeft" Language="fa-IR">     <StackPanel Margin="10">         <TextBlock  FlowDirection="LeftToRight">LRM</TextBlock>         <TextBlock><Span FlowDirection="LeftToRight">0 1 2 3 4 5 6 7 8 9</Span></TextBlock>     </StackPanel>     <StackPanel Margin="10">         <TextBlock FlowDirection="LeftToRight">LRE & PDF</TextBlock>         <TextBlock>[arabic text]<Run FlowDirection="LeftToRight">0 1 2 3 4 5 6 7 8 9</Run>[arabic text]</TextBlock>     </StackPanel>     <StackPanel Margin="10">         <TextBlock FlowDirection="LeftToRight">LRO</TextBlock>         <TextBlock><Run FlowDirection="LeftToRight">0 1 2 3 4 5 6 7 8 9</Run></TextBlock>     </StackPanel> </StackPanel> 

 

I hope this was helpful!

Cheers,
Helge Mahrt