2015-05-04 67 views
1

一個RichTextBox我有一組文字,我想放一個RichTextBox肚裏像這樣:標+下劃線內嵌在WPF

Result I want to see

所以我用一個RichTextBox,因爲它允許我做以下事情。然而

var zipCodeParagraph = new Paragraph(); 
string zipCodes = String.Empty; 

var dateRun = new Underline(new Run(DateTime.Today.DayOfWeek + ", " + CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(DateTime.Today.Month) + ' ' + DateTime.Today.Day)); 
Underline dateSuperscript; 

switch (DateTime.Today.Day % 10) 
{ 
    case 1: 
     dateSuperscript = new Underline(new Run("st")); 
     break; 
    case 2: 
     dateSuperscript = new Underline(new Run("nd")); 
     break; 
    case 3: 
     dateSuperscript = new Underline(new Run("rd")); 
     break; 
    default: 
     dateSuperscript = new Underline(new Run("th")); 
     break; 
} 

dateSuperscript.BaselineAlignment = BaselineAlignment.Superscript; 

if (ZipCodes.Any()) 
{ 
    zipCodeParagraph.Inlines.Add(new Run("The following zip codes are facing a ")); 
    zipCodeParagraph.Inlines.Add(new Underline(new Run("Severe Weather Threat"))); 
    zipCodeParagraph.Inlines.Add(new Run(" on ")); 
    zipCodeParagraph.Inlines.Add(dateRun); 
    zipCodeParagraph.Inlines.Add(dateSuperscript); 
    zipCodes = String.Join(", ", ZipCodes.ToArray()); 
} 

的結果是,像這樣:

Result I am getting

的問題是,改變文本的基線時要標/下標,然後到高度下劃線也會改變。我希望下劃線保持在原來的位置,並且還要發生超級腳本。

我發現只有一個關閉的解決方案,它不會以編程方式執行它here

回答

1

我試圖轉換鏈接中提到的相同代碼here。 請參閱下面的代碼。

FlowDocument mcFlowDoc = new FlowDocument(); 
     Hyperlink hyp = new Hyperlink(); 
     hyp.Foreground = Brushes.Black; 
     TextBlock txt = new TextBlock(); 
     txt.Foreground = Brushes.Black; 
     txt.Text = "Friday,April 10";   
     Run rn = new Run("th"); 
     rn.BaselineAlignment = BaselineAlignment.Superscript; 
     txt.Inlines.Add(rn); 
     hyp.Inlines.Add(txt);    
     Paragraph para = new Paragraph(); 
     para.Inlines.Add(new Run("The following zip codes are facing a ")); 
     para.Inlines.Add(new Underline(new Run("Severe Weather Threat"))); 
     para.Inlines.Add(new Run(" on ")); 
     para.Inlines.Add(hyp); 
     mcFlowDoc.Blocks.Add(para); 
     RichTextBox mcRTB = new RichTextBox(); 
     mcRTB.Width = 560; 
     mcRTB.Height = 100; 
     mcRTB.Document = mcFlowDoc; 
+0

工作正常!非常感謝你! :) – AzzamAziz

1

由於這似乎在RichTextBox的限制,最好的解決辦法是在你鏈接即而不是使用普通信件,用他們的Unicode superscript variants,問題的second answer提出的一個:

「ST」成爲 「ˢᵗ」
「ND」 變爲 「ⁿᵈ」

你也應該刪除基線設置:

//dateSuperscript.BaselineAlignment = BaselineAlignment.Superscript;