我如何将段落数据绑定到文本块? [英] How would I databind a Paragraph to a TextBlock?

查看:27
本文介绍了我如何将段落数据绑定到文本块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How would I take a Paragraph object and databind them to the TextBlock for use in a DataTemplate? A plain bind does nothing, just a ToString() of the Paragraph object.

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

The InLines property would let me add a list of TextRun's that make up the Paragraph manually, but that can't be bound to and I could really do with a binding based solution.

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

推荐答案

这是一个使用嵌套 ItemsControl 的示例.不幸的是,它会为每个 Inline 创建一个 TextBlock,而不是将整个 Paragraph 放入一个 TextBlock:

Here's an example using a nested ItemsControl. Unfortunately, it will make one TextBlock per Inline instead of putting the whole Paragraph into one 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,或者 做这个人所做的并从TextBlock派生,以便可以绑定Inlines属性.

If you want one Paragraph per element you should probably do as suggested and use a read-only RichTextBox, or do what this person did and derive from TextBlock so that the Inlines property can be bound.

这篇关于我如何将段落数据绑定到文本块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆