c#rtb:Bolden inline重写另一个bolden inline

时间:2016-01-31 12:24:35

标签: c# regex windows-10-universal

我的projekt是Windows 10通用应用程序!

我几乎有两个月的问题,当我在一个rtb中加粗两个单词时,第二个会覆盖第一个粗体内联。

示例:

我想大胆一点:

Hello; Bye

来自rtb的文字:

Hello and Bye

现在我用正则表达式搜索,在rtb中,天气是“你好”/“再见”。 每次在rtb中都有“Hello”时,我在同一个位置插入一个带有文本“Hello”的内联线,而不是“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) });
       }
}

接下来是相同的代码再见。

现在的问题是,我清除了第一个粗体内联( Hello ),同时插入第二个粗体内联(再见)。

有没有人知道在rtb中使用粗体字或者改进代码的建议?我几乎尝试了一切,但没有真正有用......

1 个答案:

答案 0 :(得分: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