簡體   English   中英

如何讓 TextBlock 在 FlowDocumentScrollViewer 內包裹或滾動

[英]How to get TextBlock to Wrap or scroll inside FlowDocumentScrollViewer

我在 4 行 x 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<string> 並且看起來不錯並且可以正確垂直滾動。 但是,當 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}" />

帶有不適合但不換行的文本的窗口的屏幕截圖一旦窗口變寬,您就可以看到整個消息

用這個

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

您可以通過使用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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM