4

ParagraphDataTemplate で使用するために、オブジェクトを取得して TextBlock にデータバインドするにはどうすればよいですか? 単純なバインドは何もせず、ToString()Paragraph オブジェクトの 1 つだけです。

InLines プロパティを使用すると、Paragraph を構成する TextRun のリストを手動で追加できますが、それをバインドすることはできず、バインディング ベースのソリューションで実際に行うことができます。

本当に必要なことに焦点を当てるために質問を編集しました。

4

5 に答える 5

3

ネストされたItemsControlを使用した例を次に示します。残念ながら、段落全体を1つのTextBlockに入れるのではなく、インラインごとに1つの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>

要素ごとに1つの段落が必要な場合は、提案どおりに実行して読み取り専用のRichTextBoxを使用するか、この人が行ったことを実行してTextBlockから派生し、Inlinesプロパティをバインドできるようにする必要があります。

于 2009-02-02T22:54:01.680 に答える
3

同様のニーズがあり、Andy's answer の行に沿って解決しました... 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>

これの 1 つの欠点は、単一のルート インラインのみをテキスト ブロックにバインドできることです。これは、コンテンツがすべてトップレベルのスパンにラップされているため、私の状況ではうまく機能しました。

于 2009-03-11T23:13:02.810 に答える
1

段落をTextBlockのインラインに直接バインドできるかどうかはわかりません。ただし、RunのTextプロパティにバインドできるクラスBindableRunを見つけることができました。代わりにそれはあなたのために働きますか?

編集:編集された質問を反映するように私の答えを変更しました。

于 2009-01-29T20:06:14.813 に答える
0

それぞれを独自のFlowDocumentでラップするParagraphオブジェクト用の独自のDataTemplateを作成して、RichTextBoxを介して提示することができます(もちろん、読み取り専用)。

于 2009-01-28T01:07:56.863 に答える
0

私はほとんど同じ問題を抱えていて、ジョシュペリーと同様の方法で答え、TextBlock をサブクラス化してインラインをバインド可能にしました。さらに、xaml マークアップの String と InlineCollection の間のコンバーターを作成しました。

書式設定されたテキストを含むリソースに TextBlock をバインドする方法は?

于 2011-04-08T09:06:28.593 に答える