2012-06-10 66 views
0

(因为这个问题一直没有得到任何答复,我重新措辞)WPF中丰富的文本框中删除隐藏字符

在我的申请,我有拥有丰富的文本框的对话框中,盒子里充满了从Twitter收集的Tweet。使用推文实体,我对推文进行格式化,以便在推文,提及和主题标签中包含内联超链接。但是,超链接永远无法正确定位;总是2或3个字符太快太远。

这是我使用设置在格式文本框中的文本代码:

TweetText.Document.ContentEnd.InsertTextInRun(Status.Text) 
Dim FlowDocument As FlowDocument = TweetText.Document 
If LinkEntity.Count > 0 Then 
      For Each Entity As Entities.TwitterUrlEntity In LinkEntity 
       Dim Start As TextPointer = FlowDocument.ContentStart 
       Dim StartPosition As TextPointer 
       Dim EndPosition As TextPointer 
       If Entity.StartIndex = 0 Then 
        StartPosition = Start.GetPositionAtOffset(Entity.StartIndex) 
       Else 
        StartPosition = Start.GetPositionAtOffset(Entity.StartIndex) 
       End If 
       EndPosition = Start.GetPositionAtOffset(Entity.StartIndex + Entity.DisplayUrl.Length, LogicalDirection.Backward) 
       Dim h As New Hyperlink(StartPosition, EndPosition) 
       AddHandler h.MouseLeftButtonDown, AddressOf Hyperclick_Link 
       h.NavigateUri = New Uri(Entity.Url) 
       h.Cursor = Cursors.Hand 
      Next 
     End If 
'I have the other entities here, they have very similar code' 
TweetText.Document = FlowDocument 

这是我的格式文本框XAML:

<RichTextBox Name="TweetText" Margin="5" FontSize="14" BorderThickness="0" IsReadOnly="True" /> 

这是输出:

The problem!

推文实体对每个实体都有适当的索引,但我确实认为Ri ch文本框具有导致此偏移量的隐藏字符。

回答

1

有趣的是,没有人回答这个问题,但我有点得到它,因为RichTextBoxes是非常讨厌使用。我目前也遇到了麻烦。

所以,你是对的,RichTextBox的确使用隐藏字符,但你不应该试图去除它们,因为它们帮助它按照它的方式工作。索引时只需要计算字符符号,而不是其他不可见的标签和符号。

我对VB并不太好,但是你应该可以使用for循环并且只有在YourTextPointer.GetPointerContext(LogicalDirection.Forward)TextPointerContext.Text时才能增加索引,否则你只需跳过它。

这样你的索引将匹配文本中的索引。

+0

好吧,我会试试看。我现在远离Windows电脑,但当我回到一台电脑时,我会测试你的理论。谢谢! –