如何让 TextBlock 在 FlowDocumentScrollViewer 内换行或滚动
我在 4 行 2 列网格内有以下 XAML。 Grid.ColumnDefinitions 将 ColumnDefinition Width 设置为 *。
<FlowDocumentScrollViewer Style="{StaticResource myFlowDoc}"
Grid.Row="4"
Grid.Column="1" >
<FlowDocument >
<Paragraph LineHeight="12" >
<ItemsControl ItemsSource="{Binding ReceivedData, Mode=OneWay}" />
<TextBlock TextWrapping="Wrap" Text="{Binding /, Mode=OneWay}" />
</Paragraph>
</FlowDocument>
</FlowDocumentScrollViewer>
数据来自 ObservaleCollection
如果这里重要的是样式 myFlowDoc
<Style x:Key="myFlowDoc">
<Setter Property="Control.Padding"
Value="0" />
<Setter Property="FlowDocumentScrollViewer.IsToolBarVisible"
Value="True" />
<Setter Property="Control.MinHeight"
Value="150" />
<Setter Property="Control.BorderBrush"
Value="SteelBlue" />
<Setter Property="Control.BorderThickness"
Value="2" />
<Setter Property="Control.VerticalAlignment"
Value="Stretch" />
</Style>
[编辑 1] 这是全屏,其中包含应换行的错误消息。在这张图片下面,我只显示了消息详细信息区域,窗口更宽,这样您就可以看到整个消息。我还将用户控件的整个 xaml 放在 https://gist.github.com/1036178#
[编辑2.1] @Navid 的建议间接让我找到了答案。删除“/”并将内容包装在数据模板中似乎可以解决问题。这是新的 XAML
<DataTemplate x:Key="StringCollection">
<TextBlock TextWrapping="Wrap" Text="{Binding}" TextAlignment="Left"/>
</DataTemplate>
<!--... now down in the ItemsControl-->
<ItemsControl ItemsSource="{Binding ReceivedData, Mode=OneWay}"
ItemTemplate="{StaticResource StringCollection}" />
I have the following XAML inside a 4 Row by 2 column grid. The Grid.ColumnDefinitions have both ColumnDefinition Width's set to *.
<FlowDocumentScrollViewer Style="{StaticResource myFlowDoc}"
Grid.Row="4"
Grid.Column="1" >
<FlowDocument >
<Paragraph LineHeight="12" >
<ItemsControl ItemsSource="{Binding ReceivedData, Mode=OneWay}" />
<TextBlock TextWrapping="Wrap" Text="{Binding /, Mode=OneWay}" />
</Paragraph>
</FlowDocument>
</FlowDocumentScrollViewer>
The data comes from an ObservaleCollection<string> and looks fine and scrolls vertically correctly. However, when one item doesn't fit horizontally in a TextBlock, the text block will not wrap and the FlowDocumentScrollViewer will not show scroll bars. The only way to see the text is to expand the window Horizontally. Does anyone know what I'm doing wrong and why the TextWrapping setting isn't honored?
In case it matters here is the style myFlowDoc
<Style x:Key="myFlowDoc">
<Setter Property="Control.Padding"
Value="0" />
<Setter Property="FlowDocumentScrollViewer.IsToolBarVisible"
Value="True" />
<Setter Property="Control.MinHeight"
Value="150" />
<Setter Property="Control.BorderBrush"
Value="SteelBlue" />
<Setter Property="Control.BorderThickness"
Value="2" />
<Setter Property="Control.VerticalAlignment"
Value="Stretch" />
</Style>
[EDIT 1]
Here is the full screen with an error message that should wrap. Below this image I have one showing just the message detail area with the window wider so you can see the entire message. I also put the entire xaml for the user control at https://gist.github.com/1036178#
[EDIT 2.1]
@Navid's suggestion led me to the answer indirectly. Removing the "/" and wrapping things in a data template seemed to do the trick. Here's the new XAML
<DataTemplate x:Key="StringCollection">
<TextBlock TextWrapping="Wrap" Text="{Binding}" TextAlignment="Left"/>
</DataTemplate>
<!--... now down in the ItemsControl-->
<ItemsControl ItemsSource="{Binding ReceivedData, Mode=OneWay}"
ItemTemplate="{StaticResource StringCollection}" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用这个
use this
您可以使用
ListView
作为引入滚动条You can introduce a scrollbar by utilizing the
ListView
as