使用 WPF WrapPanel 换行并右对齐文本 - WPF Bug?

发布于 2024-09-10 18:58:04 字数 1195 浏览 0 评论 0原文

我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哭了丶谁疼 2024-09-17 18:58:04

请尝试以下操作:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <TextBox Name="input" />
    <WrapPanel Grid.Row="2" HorizontalAlignment="Right" >
        <TextBlock Text="*" Margin="0,0,3,0"/>
        <TextBlock Text="{Binding ElementName=input, Path=Text}" TextWrapping="Wrap"/>

    </WrapPanel>
</Grid>

FlowDirection 用于支持从右向左阅读的语言。由于我不知道此类语言的规则,因此我不会假装理解为什么您会看到自己的样子,或者这是否是一个错误。也就是说,我知道更改 FlowDirection 并不是处理从左到右语言右对齐的正确方法,您应该使用 Horizo​​ntalAlignment 代替。

(为了将来参考,您可以通过Connect 站点向 Microsoft 提交错误)

Try this instead:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <TextBox Name="input" />
    <WrapPanel Grid.Row="2" HorizontalAlignment="Right" >
        <TextBlock Text="*" Margin="0,0,3,0"/>
        <TextBlock Text="{Binding ElementName=input, Path=Text}" TextWrapping="Wrap"/>

    </WrapPanel>
</Grid>

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)

对不⑦ 2024-09-17 18:58:04

在 .Net 4 (WPF4) 中,运行是可绑定的,因此您可以尝试如下操作:

<TextBlock TextAlignment="Right" TextWrapping="Wrap">
    <Run Text="*" /><Run Text="{Binding ElementName=Input, Path=Text}" />
</TextBlock>

两个 Run 元素位于同一行,因为标记边界之间的任何类型的空白都会导致两个运行之间出现空格。 (就像 HTML 一样。)

In .Net 4 (WPF4) runs are bindable, so you can try something like so:

<TextBlock TextAlignment="Right" TextWrapping="Wrap">
    <Run Text="*" /><Run Text="{Binding ElementName=Input, Path=Text}" />
</TextBlock>

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文