如何让TextBlock在FlowDocumentScrollViewer中包装或滚动

时间:2011-06-19 04:55:47

标签: wpf flowdocument textblock flowdocumentscrollviewer

我在4 Row by 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&lt; string&gt;并且看起来很好并且正确地垂直滚动。但是,当一个项目不能在TextBlock中水平放置时,文本块将不会换行,并且FlowDocumentScrollViewer将不显示滚动条。查看文本的唯一方法是水平扩展窗口。有谁知道我做错了什么以及为什么TextWrapping设置不受尊重?

如果重要的是风格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}" />

Screenshot of window with text that doesn't fit but doesn't wrap Once the window is wider you can see the whole message

2 个答案:

答案 0 :(得分:2)

使用此

<ItemsControl ItemsSource="{Binding ReceivedData, Mode=OneWay}">     
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock TextWrapping="Wrap" Text="{Binding /, Mode=OneWay}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

答案 1 :(得分:0)

您可以使用ListView作为

来引入滚动条
<Section Name="Gallery">
                <Paragraph>
                    <ListView ItemsSource="{Binding GalleryCards}"
                              BorderBrush="Transparent"
                              HorizontalAlignment="Center"
                              ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                              Padding="10"
                              Width="{Binding ElementName=Viewer, Path=RenderSize.Width, Converter={StaticResource DocumentSizeConverter}, ConverterParameter=80, UpdateSourceTrigger=PropertyChanged}">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ContentControl s:View.Model="{Binding .}"/>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                        <ListView.ItemsPanel>
                            <ItemsPanelTemplate>
                                <WrapPanel HorizontalAlignment="Center" />
                            </ItemsPanelTemplate>
                        </ListView.ItemsPanel>
                    </ListView>
                </Paragraph>
            </Section>