代码之家  ›  专栏  ›  技术社区  ›  VolkerK

如何将段落数据绑定到文本块?

  •  4
  • VolkerK  · 技术社区  · 17 年前

    我该怎么办 Paragraph 对象并将其数据绑定到TextBlock,以便在DataTemplate中使用? 简单的捆绑什么也不做,只是 ToString() 段落对象。

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

    编辑问题,重点放在我真正需要做的事情上。

    5 回复  |  直到 17 年前
        1
  •  3
  •   Robert Macnee    17 年前

    这是一个使用嵌套的Items控件的示例。不幸的是,它会为每个内联创建一个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 以便可以绑定Inline属性。

        2
  •  2
  •   joshperry    16 年前

    我也有类似的需求,并按照安迪的回答解决了这个问题。..我创建了一个BindableTextBlock:

    class BindableTextBlock : TextBlock
    {
        public Inline BoundInline
        {
            get { return (Inline)GetValue(BoundInlineProperty); }
            set { SetValue(BoundInlineProperty, value); }
        }
    
        public static readonly DependencyProperty BoundInlineProperty =
            DependencyProperty.Register("BoundInline", typeof(Inline), typeof(BindableTextBlock),
                new UIPropertyMetadata((PropertyChangedCallback)((d, e) => { ((BindableTextBlock)d).Inlines.Clear(); ((BindableTextBlock)d).Inlines.Add(e.NewValue as Inline); })));
    }
    

    然后,在XAML中,我可以绑定到BoundInline依赖属性:

        <DataTemplate x:Key="TempTemplate">
            <t:BindableTextBlock TextWrapping="Wrap" BoundInline="{Binding Path=TextInlines}" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="2" />
        </DataTemplate>
    

    这样做的一个缺点是,你只能将一个根内联绑定到文本块,这对我的情况来说很好,因为我的内容都封装在顶级Span中。

        3
  •  1
  •   Andy    17 年前

    我不确定是否可以将段落直接绑定到TextBlock的内联。然而,我还是找到了那个班 BindableRun 它允许您绑定到Run的Text属性。那对你有用吗?

    编辑:修改我的答案以反映编辑后的问题。

        4
  •  0
  •   Christian Klauser    17 年前

    您可以尝试为段落对象创建自己的DataTemplate,将每个对象包装在自己的FlowDocument中,然后通过RichTextBox(当然是只读的)呈现

        5
  •  0
  •   Community Mohan Dere    8 年前

    我遇到了几乎相同的问题,并以类似于joshperry的方式回答了它,对TextBlock进行了子类化,使内联线可绑定。此外,我编写了一个在xaml标记字符串和内联集合之间的转换器。

    How to bind a TextBlock to a resource containing formatted text?