5

私はこのXAMLを持っています:

<TextBlock TextWrapping="Wrap" Foreground="Green"
           Text="This is some Green text up front. ">
    <TextBlock Foreground="Blue">
        This is some Blue text.
    </TextBlock>
    This is some Green text following the Blue text. 
    <Hyperlink>
        <TextBlock Text="And finally, this is a Hyperlink." TextWrapping="Wrap"/>
    </Hyperlink>
</TextBlock>  

そして、C#で手続き的に複製する方法を知りたいです。

TextBlock次のようなものを使用して C# で sを作成する方法を知っています。

TextBlock tb = new TextBlock();
tb.Text="Some text"

TextBlockまた、C# 内のパネルに複数の をまとめることができます。TextBlockしかし、 s を他TextBlockの sに入れたり、 TextBlocks をHyperlinksに入れたりする方法がわかりませんTextBlock

どういうわけか、いくつかのコンテナ オブジェクトと追加TextBlockオブジェクトが自動的に作成されていますか? またはTextBlock、他のアイテムを含めることができるいくつかのメソッド/プロパティがありますか?

その他の関連する質問:
1. Click() イベントのようなものを に追加する最良の方法は何Hyperlinkですか?
2. 青いテキストをよりきれいに折り返す方法はありますか? 上記の XAML では、右端の単語を折り返す必要があるとすぐに、代わりに青いテキストのブロック全体が折り返されます。

提供できるイルミネーションに感謝します。

4

2 に答える 2

7

TextBlock の Inlines プロパティを介して公開された Inlines のコレクションを変更できます。上記の XAML サンプルは、次のようになります。

TextBlock tb = new TextBlock
               {
                  Text = "This is some Green text up front.",
                  Foreground = Brushes.Green
               };

InlineCollection tbInlines = tb.Inlines;

tbInlines.Add(new Run
              {
                 Text = "This is some Blue text.",
                 TextWrapping = TextWrapping.Wrap,
                 Foreground = Brushes.Blue
              });

tbInlines.Add(new Run
              {
                 Text = "This is some Green text following the Blue text."
              });

Run hyperlinkRun = new Run("And finally, this is a Hyperlink.");

tbInlines.Add(new Hyperlink(hyperlinkRun));

関連する質問について:

1A) クラスの RequestNavigate イベントを使用して、すべての Hyperlink インスタンスにイベント ハンドラーをフックすることは可能ですが、セットアップにコストがかかり、必要以上のメモリを使用する可能性があります。代わりに、ルーティング イベントを活用し、すべてのハイパーリンクが配置されるコンテナーで RequestNavigate イベントを単純にフックすることをお勧めします。これは次のように行うことができます。

myContainer.AddHandler(
                     Hyperlink.RequestNavigateEvent, 
                     new RequestNavigateEventHandler(
                                                      (sender, args) =>
                                                      {
                                                        /* sender is the instance of the Hyperlink that was clicked here */
                                                      }));

2A) XAML の例では、内側の TextBlock を、まとめてラップする必要がある要素として扱っています。私の実行ベースのアプローチを使用している場合、ラッピングは含まれているテキスト ブロックから継承する必要があります。

于 2009-10-15T16:24:20.897 に答える
1

TextBlocksについてはわかりませんが、RichTextBoxで行う方法は次のとおりです。TextBlockの代わりにRichTextBoxを使用できます。

 RichTextBox rtbTest = new RichTextBox();
 rtbTest.IsDocumentEnabled = true;

 FlowDocument fd = new FlowDocument();
 Paragraph para = new Paragraph();

 Run r4 = new Run("Some Text To Show As Hyperlink");
 Hyperlink h4 = new Hyperlink(r4);
 h4.Foreground = Brushes.Red; //whatever color you want the HyperLink to be

 // If you want the Hyperlink clickable
 h4.NavigateUri = new Uri("Some URL");
 h4.RequestNavigate += new RequestNavigateEventHandler(h_RequestNavigate);
 // Leaving the two previous lines out will still make the Hyperlink, but it won't be clickable

 // use this if you don't want an underline under the HyperLink
 h4.TextDecorations = null;

 para.Inlines.Add(h4);

 fd.Blocks.Add(para);

 rtbTest.Document = fd;

通常のテキストでは、ハイパーリンクを使用しましたが、クリック可能にする2行を削除しました。これにより、テキストに色が付けられますが、クリックできなくなります。それでもカーソルは変わりますが。それを変える性質もあると思いますが。

于 2009-10-15T16:19:34.337 に答える