2016-01-31 34 views
0

我的項目是一個Windows 10通用應用程序!c#rtb:Bolden inline覆蓋另一個bolden內嵌

我差不多有兩個月的問題,當我在一個rtb中加粗兩個單詞時,第二個覆蓋第一個粗體內聯。

例子:

我想大膽:

Hello; Bye 

從RTB文字:

Hello and Bye 

現在我用正則表達式搜索,天氣療法是 「你好」/ 「再見」,在RTB。 每次在rtb中都會出現「Hello」,我會在與「hello」站點相同的位置插入bolden內嵌文本「Hello」。

之後,我做了與「再見」相同。

我的代碼:

string text = run.Text; -> "Hello and Bye" 

MatchCollection mc = Regex.Matches(text, "Hello", RegexOptions.Multiline); 

int i = 0; 
var bold = new Bold(); 
int iIn = 0; 
int iLe = 0; 
p.Inlines.Clear(); -> p = Paragraph from rtb 

foreach (Match match in mc) 
{ 
     p.Inlines.Add(new Run { Text = text.Substring(i, match.Index - i) }); 
     bold.Inlines.Add(new Run { Text = text.Substring(match.Index, match.Length) }); 
     p.Inlines.Add(bold); 
     i = match.Index + match.Length; 

     if (i < text.Length) 
     { 
      p.Inlines.Add(new Run { Text = text.Substring(i) }); 
     } 
} 

其次是與再見相同的代碼。

現在的問題是,我清除首次大膽在線(你好),而插入第二個大膽的內聯(再見)。

有沒有人知道在rtb中用粗體表示一個特定詞的替代方案還是改善代碼的建議?我幾乎tryed一切,但沒有真正的工作...

+0

你叫'p.Inlines.Clear()'一次或兩次? –

+0

@MatthewStrawbridge我把它叫做bevore,在hello和bye中加入bolden inline。 –

+0

@ehh這聽起來不錯,但我不能將int轉換爲Windows.UI.Xaml.Documents.TextPointer(註釋回答) –

回答

0

使用以下,以選擇相關字符,大膽:」

public void Select(
    TextPointer start, 
    TextPointer end 
) 

In order to get TextPointer, try this (not checked): 

TextPointer pointer = document.ContentStart; 

TextPointer start = pointer.GetPositionAtOffset(0); 
TextPointer end = start.GetPositionAtOffset(5); 

rtb.Select (start,end); // for example to select Hello 
       // Then change the font to bold 
+0

In我的Project Visual Studio說,rtb.selectionstart只能獲取值?我如何製作它? –

+0

看起來,我添加了一張顯示SelectionStart具有getter和setter的圖片。 – ehh

+0

@DieterMüller,您的富文本框所屬的程序集? – ehh