7

私はそれが、例えば、をTextBlock提示できることを知っています:FlowDocument

<TextBlock Name="txtFont">
     <Run Foreground="Maroon" FontFamily="Courier New" FontSize="24">Courier New 24</Run>
</TextBlock>

FlowDocument変数に格納されているをに設定する方法を知りたいのですがTextBlock。私は次のようなものを探しています:

string text = "<Run Foreground="Maroon" FontFamily="Courier New" FontSize="24">Courier New 24</Run>"
txtFont.Text = text;

ただし、上記のコードの結果は、XAMLテキストが解析されずに表示されることです。


編集:私の質問は十分に明確ではなかったと思います。私が本当に達成しようとしているのは、次のとおりです。

  1. ユーザーはRichTextBoxにテキストを入力します。
  2. アプリケーションは、 RichTextBoxFlowDocumentからのユーザー入力を保存し、ディスクにシリアル化します。
  3. FlowDocumentディスクから変数テキストに逆シリアル化されます。
  4. ここで、ユーザーテキストをで表示できるようにしたいと思いますTextBlock

したがって、私が理解している限り、新しいRunオブジェクトを作成し、パラメーターを手動で設定しても、問題は解決しません。


問題は、RichTextBoxをシリアル化するとSectionオブジェクトが作成され、 TextBlock.Inlinesに追加できないことです。したがって、逆シリアル化されたオブジェクトをTextBlockのTextPropertyに設定することはできません。

4

4 に答える 4

5

以下のようにオブジェクトを作成して追加します。

        Run run = new Run("Courier New 24");
        run.Foreground = new SolidColorBrush(Colors.Maroon);
        run.FontFamily = new FontFamily("Courier New");
        run.FontSize = 24;
        txtFont.Inlines.Add(run);
于 2009-11-04T11:37:37.577 に答える
3

TextBlockがFlowDocumentを提示できることを知っています

何があなたをそう思わせたのですか ?私はそれが本当だとは思わない...aの内容はTextBlockプロパティInlinesであり、それはInlineCollectionInlineしたがって、 s ...のみを含めることができます。ただし、のFlowDocument場合、コンテンツはBlocksプロパティであり、。のインスタンスが含まれますBlock。そして、aBlockInline

于 2009-11-04T13:12:43.453 に答える
0

FlowDocument逆シリアル化されている場合、それはタイプのオブジェクトがあることを意味しますよFlowDocumentね?TextBlockのTextプロパティをこの値に設定してみてください。もちろん、これtxtFont.Text = ...は文字列に対してのみ機能するため、でこれを行うことはできません。他のタイプのオブジェクトの場合、DependencyPropertyを直接設定する必要があります。

txtFont.SetValue(TextBlock.TextProperty, myFlowDocument)
于 2009-11-04T12:39:28.463 に答える
0

これが、オンザフライでスタイルを割り当てることによってテキストブロックの外観を設定する方法です。

    // Set Weight (Property setting is a string like "Bold")
    FontWeight thisWeight = (FontWeight)new FontWeightConverter().ConvertFromString(Properties.Settings.Default.DealerMessageFontWeightValue);

    // Set Color (Property setting is a string like "Red" or "Black")
    SolidColorBrush thisColor = (SolidColorBrush)new BrushConverter().ConvertFromString(Properties.Settings.Default.DealerMessageFontColorValue);

    // Set the style for the dealer message
    // Font Family Property setting  is a string like "Arial"
    // Font Size Property setting is an int like 12, a double would also work
    Style newStyle = new Style
    {
        TargetType = typeof(TextBlock),
        Setters = {
            new Setter 
            {
                Property = Control.FontFamilyProperty,
                Value = new FontFamily(Properties.Settings.Default.DealerMessageFontValue)
            },
            new Setter
            {
                Property = Control.FontSizeProperty,
                Value = Properties.Settings.Default.DealerMessageFontSizeValue
            },
            new Setter
            {
                Property = Control.FontWeightProperty,
                Value = thisWeight
            },
            new Setter
            {
                Property = Control.ForegroundProperty,
                Value = thisColor
            }
        }
    };

    textBlock_DealerMessage.Style = newStyle;

スタイルセクションを削除してプロパティを直接設定することもできますが、プロジェクト全体で外観を整理できるように、スタイルにバンドルされたままにしておくのが好きです。

textBlock_DealerMessage.FontWeight = thisWeight;

HTH。

于 2011-05-02T00:04:58.100 に答える