2

RichTextBox私は最近、文字列とその文字列に関するいくつかのプロパティを保持するオブジェクトのコレクションにバインドされた段落を表示するのが良い解決策だと思うプロジェクトに取り組んでいました。私はこのようなことができることを望んでいました:

<RichTextBox Name="rtb" IsReadOnly="True" FontSize="14" FontFamily="Courier New">
    <FlowDocument>
        <Paragraph>
            <Run Foreground="Red" Text="{Binding foo}"/>
            <Run Foreground="Green" Text="{Binding bar}"/>
            <Run Foreground="Blue" Text="{Binding baz}"/>
        </Paragraph>
    </FlowDocument>
</RichTextBox>

これは問題なく動作しますが、これは事前にすべてのバインディングを定義していますが、実際には実行時までわかりません。各文字列はコレクションに格納されます。Run複数のブロックを定義してバインドし、それらをすべて に配置して表示する方法を考えようとしていましたRichTextBox。可能であれば、主に xaml ソリューションを使用したいと思いますが、コード ビハインドでそれを行う必要がある場合は、バインディングが保持され、初期ロード後にすべてが適切に更新される限り、問題ない可能性があります。

私はこのようなことを試しましたが、コードビハインドからTexta のプロパティにバインドする方法を理解するのに苦労していました:Run

foreach (var item in col)
{        
    Run run = new Run();
    Binding b = new Binding();
    b.Source = this;
    b.Path = new PropertyPath(item.StaticText);
    run.SetBinding(run.Text, b);    //Tells me Text is not a dependency property
                                    //But it could be bound if this were xaml?
}

また、アイテムのデータ テンプレートとして持ってItemsControlいるを入れてみましたが、データ テンプレートをタグにすることはできず、とにかくうまくいくかどうかわかりません。RichTextBoxRunRun

最善のアプローチに関するガイダンス、または私のソリューションの問題点。

4

2 に答える 2

1

あなたはこれを使うことができます...

<Paragraph>
    <ItemsControl ItemsSource="{Binding MyRuns}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBlock>
                    <Run Foreground="{Binding Color}" Text="{Binding Text}"/>
                </TextBlock>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate >
                <StackPanel Orientation="Vertical" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
    <!--<Run Foreground="Red" Text="{Binding foo}"/>
    <Run Foreground="Green" Text="{Binding bar}"/>
    <Run Foreground="Blue" Text="{Binding baz}"/>-->
</Paragraph>

MyRunsビューモデルのコレクションとして 定義します。

public ObservableCollection<RunData> MyRuns { get; set; }  

あなたのRunDataetc にプロパティを入れてください。

もちろんItemsControl.ItemsPanel、 を削除することもできます。これは楽しみのためです

于 2013-04-11T12:47:59.847 に答える
0

これで仕事が終わったようです。を としてItemsControl使用するを作成しました。次に、各アイテムを文字列のコレクションにバインドすると、可変文字列の素敵な段落のように見えます。誰かがよりエレガントな解決策を思いついた場合に備えて、質問を開いたままにします。WrapPanelItemsPanel

<RichTextBox Name="rtb" IsReadOnly="True">
    <FlowDocument>
        <Paragraph>
            <ItemsControl ItemsSource="{Binding col}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock>
                            <TextBlock Text="{Binding StaticText,
                                UpdateSourceTrigger=PropertyChanged}"/><TextBlock Text=" "/>
                        </TextBlock>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Paragraph>
    </FlowDocument>
</RichTextBox>
于 2013-04-11T12:28:31.290 に答える