wpf - 如何将段落数据绑定(bind)到 TextBlock?

标签 wpf data-binding xaml textblock paragraph

我将如何采取Paragraph对象并将它们数据绑定(bind)到 TextBlock 以在 DataTemplate 中使用?
一个普通的绑定(bind)什么都不做,只是一个 ToString()段落对象。

InLines 属性可以让我手动添加组成段落的 TextRun 的列表,但这不能绑定(bind),我真的可以使用基于绑定(bind)的解决方案。

编辑问题以专注于我真正需要做的事情。

最佳答案

下面是一个使用嵌套 ItemsControl 的示例。不幸的是,它会为每个内联创建一个 TextBlock,而不是将整个段落放入一个 TextBlock:

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:sys="clr-namespace:System;assembly=mscorlib"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid.Resources>
        <FlowDocument x:Key="document">
            <Paragraph><Run xml:space="preserve">This is the first paragraph.  </Run><Run>The quick brown fox jumps over the lazy dog.</Run></Paragraph>
            <Paragraph><Run xml:space="preserve">This is the second paragraph.  </Run><Run>Two driven jocks help fax my big quiz.</Run></Paragraph>
            <Paragraph><Run xml:space="preserve">This is the third paragraph.  </Run><Run>Sphinx of black quartz, judge my vow!</Run></Paragraph>
        </FlowDocument>
        <DataTemplate DataType="{x:Type Paragraph}">
            <ItemsControl ItemsSource="{Binding Inlines}" IsHitTestVisible="False">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </DataTemplate>
    </Grid.Resources>
    <ListBox ItemsSource="{Binding Blocks, Source={StaticResource document}}"/>
</Grid>

如果您希望每个元素一个段落,您可能应该按照建议执行并使用只读 RichTextBox 或 do what this person did and derive from TextBlock以便可以绑定(bind) Inlines 属性。

关于wpf - 如何将段落数据绑定(bind)到 TextBlock?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/483834/

相关文章:

wpf - 故障模块名称 : PresentationFramework. ni.dll

wpf - 将 WPF ContextMenu MenuItem 绑定(bind)到 UserControl 属性与 ViewModel 属性

android - 无法使用 Android Studio 3.0 + DataBinding + Kotlin 构建项目

Silverlight:如何在 setter 中为样式使用绑定(bind)(或等效的解决方法)

c# - 从另一个 View 的中心开始到其左边界的水平展开动画

wpf - 您可以将非字符串标记值放入 xaml 声明中的组合框吗?

C# WPF : ItemsControl with Add-Content Button

wpf - 动画 WPF 中状态之间的边距值

wpf - 使用绑定(bind)取消 WPF 中的更新

xaml - ListView 选定项绑定(bind)不起作用