title: Silverlight Richtextbox obtain cursor position
body: <p>I'm trying create a user control using the silverlight 5 richtextbox.  I need to be able to insert "inline ui" and can't work out how to get the current cursor position.</p>
  
  <p>I trigger my code like this:</p>
  
  <pre><code>this.GetAbsolutePos(this.richText.Selection.Start);
  </code></pre>
  
  <p>The guts of this method is here:</p>
  
  <pre><code>private int GetAbsolutePos(TextPointer textPointer)
  {
     int index = 0;
     TextPointer pos = this.richText.ContentStart;
     while (pos.CompareTo(textPointer) != 0)
     {
        if (pos.IsAtInsertionPosition)
        {
            index++;
        }
  
        pos = pos.GetNextInsertionPosition(LogicalDirection.Forward);
     }
     return index;
  }
  </code></pre>
  
  <p>Given the following text in a richtextbox control....</p>
  
  <p><img src="https://i.stack.imgur.com/ezSKp.png" alt="Empty Control"></p>
  
  <p>If the cursor is between 5 &amp; 6 on the first line, then the above function correctly returns 5.  But as the cursor is further into the text the position becomes more inaccurate.  ie between 5 &amp; 6 on the second line returns 16 and the third line returns 27.</p>
  
  <p>It also becomes more difficult as I'm inserting inline elements at these positions, which then count as a "symbol" and further cause the count to go wrong.</p>
  
  <p>This image shows what is happening when I "insert" the inline ui between 5 &amp; 6 on each line.
  <img src="https://i.stack.imgur.com/MfobK.png" alt="With 3 Inlines"></p>
  
  <p>Just for completeness here is the Xaml from richtext.Xaml (I've removed all the extra attributes from the Section/Paragraph elements to make it clearer)</p>
  
  <pre><code>&lt;Section&gt;
     &lt;Paragraph&gt;
         &lt;Run&gt;1234567890&lt;/Run&gt;
         &lt;LineBreak /&gt;
         &lt;Run&gt;1234567890&lt;/Run&gt;
         &lt;LineBreak /&gt;
         &lt;Run&gt;1234567890&lt;/Run&gt;
     &lt;/Paragraph&gt;
  &lt;/Section&gt; 
  </code></pre>
  
  <p>Based on the remarks on this page <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.textpointer%28v=vs.95%29.aspx" rel="nofollow noreferrer">MSDN Silverlight TextPointer Class</a> </p>
  
  <blockquote>
    <p>Symbol - For TextPointer operations, any of the following is considered to be a symbol:</p>
    
    <ul>
    <li><p>An opening or closing tag for a TextElement element.</p></li>
    <li><p>A UIElement element contained within an InlineUIContainer. Note that
    a UIElement is always counted as exactly one symbol. Any additional
    content or elements contained by the UIElement are not considered
    symbols.</p></li>
    <li><p>Each 16-bit Unicode character inside of a text Run element.</p></li>
    </ul>
  </blockquote>
  
  <p>I think I need to "know" what kind of "symbol" I am currently on but cannot figure out how.</p>
  
  <p>Seems like it should be easy, but working with TextPointers seems very unintuitive.
  I had an idea to parse the Xaml to find the cursor position but that seems like a real hack.</p>
  
  <p>Any help would be appreciated.</p>
  
  <p>Thanks</p>