簡體   English   中英

如何在 WPF RichTextBox 中使字符串的某些部分變為粗體並具有不同的字體樣式(大小、字體粗細)

[英]How to make some parts of a string to be bold and with different font style (size, font weight) within a WPF RichTextBox

我有以下 WPF RichTextBox,它在我的視圖 model 中綁定到一個 MVVM 屬性。屬性 SampleText 包含一個字符串,比方說,例如:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat。 Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur。 Excepteur sint occaecat cupidataat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

我想通過以下方式使其在 RichTextBox 中可見:

在此處輸入圖像描述

如上所示,我想使用相同的字體(Segoe UI)顯示所有段落,但一些單詞以粗體突出顯示,第一個短語使用更大的字體大小。

在我的代碼下面:

<RichTextBox IsReadOnly="True"
             Focusable="False">
    <FlowDocument>
         <Paragraph>
               <Run Text="{Binding SampleText}" />
         </Paragraph>
    </FlowDocument>
</RichTextBox>

在粗體的情況下,我嘗試在一些單詞周圍插入一些<Bold></Bold>對標簽,它們在 RichTextBox 中沒有顯示為粗體,而是<Bold></Bold>標簽無法識別並且它們只是按原樣顯示在 RichTextBox 中。

如果為第一行應用更大的字體大小,我不知道該怎么做,我不知道要使用的正確標簽對是什么。

我如何在 WPF 中執行此操作?

我的 SampleText 屬性是從代碼中以編程方式動態更改的,我的意思是,我根據場景為它分配不同的格式化值(我的意思是格式化,應用諸如和等的標簽)所以這將是一種只需分配即可工作的好方法該屬性的格式化值,然后它將被自動處理並正確顯示在 RichTextBox 中。

我要說的是,我確定我是通過綜合一生前的一個或多個 SO 答案得到的,但我不知道該歸功於誰,因為這段代碼至少已有 7 年歷史了。

正如我認為您現在已經意識到您實際上不能將 RTF 字符串綁定到RichTextBox (您知道,這太容易了......),所以我改為子類化並使用自定義 DP。 不過,您應該可以輕松地根據自己的需要進行調整。

    public class BindableRichTextBox : RichTextBox
    {
        #region string RichText dependency property
        public static readonly DependencyProperty RichTextProperty = 
            DependencyProperty.Register(
                "RichText", 
                typeof(string), 
                typeof(BindableRichTextBox), 
                new PropertyMetadata(
                    (string)null,
                    (obj, args) =>
                    {
                        ((BindableRichTextBox)obj).OnRichTextChanged(args);
                    }));
        public string RichText
        {
            get
            {
                return (string)GetValue(RichTextProperty);
            }
            set
            {
                SetValue(RichTextProperty, value);
            }
        }
        private void OnRichTextChanged(DependencyPropertyChangedEventArgs args)
        {
            string rtf = args.NewValue as string;
            if (string.IsNullOrEmpty(rtf))
            {
                this.Document = null;
                return;
            }
            this.Document = GetFlowDocumentFromRTF(rtf);
        }
        #endregion

        private FlowDocument GetFlowDocumentFromRTF(string text)
        {
            FlowDocument document = new FlowDocument();

            using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(text)))
            {                    
                TextRange tr = new TextRange(document.ContentStart, document.ContentEnd);
                tr.Load(ms, DataFormats.Rtf);
            }
            return document;
        }
    }

要使用它,您只需說:

<local:BindableRichTextBox IsReadOnly="True"
                           Focusable="False"
                           RichText="{Binding SampleText}"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM