Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF RichTextBox应用程序属性值_C#_Wpf - Fatal编程技术网

C# WPF RichTextBox应用程序属性值

C# WPF RichTextBox应用程序属性值,c#,wpf,C#,Wpf,我在WPF Richtextbox中突出显示所有出现的非中断空格。 找到所需的textrange后,我会调用: textrange.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.DarkRed); 而且效果很好。但是,如果突出显示发生在文档末尾,则所有新键入的文本也将突出显示,这是不好的。 有人知道如何解决这个问题吗 完整代码: private void HighLightNonbreakSpace() {

我在WPF Richtextbox中突出显示所有出现的非中断空格。 找到所需的textrange后,我会调用:

textrange.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.DarkRed);
而且效果很好。但是,如果突出显示发生在文档末尾,则所有新键入的文本也将突出显示,这是不好的。 有人知道如何解决这个问题吗

完整代码:

private void HighLightNonbreakSpace()
    {
        var start = this.Document.ContentStart;
        char nonBreakSpace = System.Convert.ToChar(160);
        while (start != null && start.CompareTo(this.Document.ContentEnd) < 0)
        {
            if (start.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
            {
                var match = start.GetTextInRun(LogicalDirection.Forward).IndexOf(nonBreakSpace);
                if (match >=0)
                {
                    var matchPos = start.GetPositionAtOffset(match, LogicalDirection.Forward);
                    var textrange = new TextRange(matchPos, matchPos.GetPositionAtOffset(1,LogicalDirection.Forward));
                    textrange.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.DarkRed);
                    start = textrange.End;
                }
            }
            start = start.GetNextContextPosition(LogicalDirection.Forward);
        }
    }
private void HighLightNonbreakSpace()
{
var start=this.Document.ContentStart;
char nonBreakSpace=System.Convert.ToChar(160);
while(start!=null&&start.CompareTo(this.Document.ContentEnd)<0)
{
if(start.GetPointerContext(LogicalDirection.Forward)=TextPointerContext.Text)
{
var match=start.gett.run(LogicalDirection.Forward).IndexOf(nonBreakSpace);
如果(匹配>=0)
{
var matchPos=start.GetPositionAtOffset(匹配,逻辑方向.Forward);
var textrange=新的textrange(matchPos,matchPos.GetPositionAtOffset(1,LogicalDirection.Forward));
textrange.ApplyPropertyValue(TextElement.BackgroundProperty,Bruss.DarkRed);
开始=文本范围。结束;
}
}
start=start.GetNextContextPosition(LogicalDirection.Forward);
}
}

您可能不仅需要高亮显示NBSP,还需要取消高亮显示其他所有内容,即在例程中添加一个else分支。默认情况下,新键入的文本将从其前面的任何内容获取其属性,因此您必须确定最后键入的字符是否为nbsp,并相应地设置其属性