我正在WPF中显示一个带有TextBlock的字符串。它带有一个句点字符,中间是一个换行符。由于某种原因,这会导致TextBlock始终在行的开头显示句点字符(它应该紧接在“内容”一词之后)。当我启用文字换行时,它变得更加陌生。现在,通过调整窗口的大小,我可以将角色移动到另一行,但始终始终在开始处。
这是我的标记:
<Grid Width="Auto">
<Grid.Style>
<Style TargetType="Grid">
<Setter Property="Margin" Value="{StaticResource MessageBubbleMarginRight}" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Message.IsMine, ElementName=owner}" Value="True">
<Setter Property="Margin" Value="{StaticResource MessageBubbleMarginLeft}" />
<Setter Property="HorizontalAlignment" Value="Right" />
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
<Rectangle RadiusX="10" RadiusY="10" Stroke="Black" Fill="AliceBlue" />
<TextBlock Text="{Binding ElementName=owner, Path=Message.Text, FallbackValue=This is a super long
Multiline text}"
Margin="20, 20, 20, 20"
TextWrapping="Wrap"
VerticalAlignment="Center">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="TextAlignment" Value="Right" />
<Style.Triggers>
<DataTrigger Binding="{Binding Message.IsMine, ElementName=owner}" Value="True">
<Setter Property="TextAlignment" Value="Left" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
我在这里想念什么吗?还是这是WPF中的渲染故障?
我刚刚找到答案。我设法在设计器中重现该错误(请参见第一个屏幕截图,句点放在“ long”之后,然后换行),并意识到消息气泡位于ItemsControl
和FlowDirection=RightToLeft
中。将其更改回LeftToRight
或在树的下方引入LTR StackPanel
即可解决此问题。 仍然,我相信这是一个错误,因为流向不应该更改文本流向(特别是弄乱了某些字符的顺序),并将其报告给开发团队。
编辑:显然,这是预期的行为,因为UI元素应该继承FlowDirection
,包括TextBlock
,导致其将其文本呈现为RTL。解决方法是简单地在FlowDirection=LeftToRight
中设置TextBlock
。有关更多信息,请参见https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/bidirectional-features-in-wpf-overview。
供以后参考,它使用.NET Core 3.0(无预览)。这段代码引起了这个问题,特别是第2行:
<ItemsControl ItemsSource="{Binding ElementName=owner, Path=Session.CurrentConversation.Value.Messages, FallbackValue=ABCDEFG}"
FlowDirection="RightToLeft">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<DockPanel LastChildFill="False" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<chat:ChatMessageTextBubble Message="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="DockPanel.Dock" Value="Bottom" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>