使用 WPF WrapPanel 换行并右对齐文本 - WPF Bug?
我尝试使用 WrapPanel 和两个 TextBlock 在某些文本的左侧附加一个星号 (*),允许文本换行,并强制文本右对齐。我已成功完成此操作,方法是创建一个 WrapPanel,将 FlowDirection 设置为 RightToLeft,然后添加我的文本,后跟星号。但是,如果我使用的文本碰巧在行尾有任何非字母数字字符,它就会莫名其妙地被强制到行的前面。我觉得这种行为很奇怪。我认为这一定是 WPF 中的错误,而不是预期的行为。
文本 = 普通文本(其他文本)的示例:
Expected:
* Normal Text (Other
Text)
Actual:
* Normal Text (Other
(Text
请随意使用以下示例代码来重新创建问题。只需将其放入高度和宽度 = 100 的窗口中,然后在文本框中键入“普通文本(其他文本)”即可。或者,将高度和宽度设置为您喜欢的任何值,并编写足够的文本以强制文本换行,然后在末尾添加标点符号。
示例代码:
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBox Name="input" />
<WrapPanel Grid.Row="2" FlowDirection="RightToLeft">
<TextBlock Text="{Binding ElementName=input, Path=Text}" TextWrapping="Wrap"/>
<TextBlock Text="*" Margin="0,0,3,0"/>
</WrapPanel>
</Grid>
那么,我的问题。
- 这是一个错误,还是有意为之?
- 如果这是一个错误,我应该以某种方式将其提交给 Microsoft 吗?如何?
自从开始这篇文章以来,我决定将两个 TextBlock 放在两列网格中。将包含非星号的 TextBlock 配置为使用 Right TextAlignment 后,无论如何我都满足了我的所有要求。尽管如此,我发现这是一个有趣的问题。
I am attempting to use the WrapPanel and two TextBlocks to append an asterisk (*) to the left side of some text, allow the text to wrap, and force the text to be right aligned. I have successfully done so by creating a WrapPanel with the FlowDirection set to RightToLeft and adding my text, followed by the asterisk. However, if the text I use happens to have any non-alphanumeric characters at the end of the line it is inexplicably forced to the front of the line. I find this behavior to be very strange. I think it must be a bug in WPF and not intended behavior.
Example with Text = Normal Text (Other Text) :
Expected:
* Normal Text (Other
Text)
Actual:
* Normal Text (Other
(Text
Feel free to use the following sample code to recreate the issue for yourself. Simply put this in a window with Height and Width = 100, then type "Normal Text (Other Text)" in the TextBox. Or, set the Height and Width to anything you like and write enough text that it is forced to wrap the text, then add punctuation to the end.
Sample Code:
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBox Name="input" />
<WrapPanel Grid.Row="2" FlowDirection="RightToLeft">
<TextBlock Text="{Binding ElementName=input, Path=Text}" TextWrapping="Wrap"/>
<TextBlock Text="*" Margin="0,0,3,0"/>
</WrapPanel>
</Grid>
So, my question(s).
- Is this a bug, or is this intended?
- If this is a bug, should I submit it to Microsoft in some way? How?
Since starting this post, I have decided to put the two TextBlocks in a two column grid instead. With the non-asterisk containing TextBlock configured to use a Right TextAlignment I meet all my requirements anyway. Still, I found this to be an interesting issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请尝试以下操作:
FlowDirection 用于支持从右向左阅读的语言。由于我不知道此类语言的规则,因此我不会假装理解为什么您会看到自己的样子,或者这是否是一个错误。也就是说,我知道更改 FlowDirection 并不是处理从左到右语言右对齐的正确方法,您应该使用 HorizontalAlignment 代替。
(为了将来参考,您可以通过Connect 站点向 Microsoft 提交错误)
Try this instead:
FlowDirection is for use to support languages that are read that from right to left. Since I don't know the rules for languages like that I'm not going to pretend to understand why you are seeing what you are, or if it is a bug. That said I know that changing the FlowDirection isn't the correct way to handle right aligning left to right languages and you should use HorizontalAlignment instead.
(For future reference you submit bugs to Microsoft through the Connect site)
在 .Net 4 (WPF4) 中,运行是可绑定的,因此您可以尝试如下操作:
两个 Run 元素位于同一行,因为标记边界之间的任何类型的空白都会导致两个运行之间出现空格。 (就像 HTML 一样。)
In .Net 4 (WPF4) runs are bindable, so you can try something like so:
The two Run elements are on the same line because any type of whitespace between the tag boundaries will cause a space to appear between the two runs. (As HTML does.)