title: how to get and set current cursor position of WPF textbox
body: <p>I want to get the current cursor position from a WPF TextBox. If a <code>TextBox</code> contains text <code>abhishek</code> and cursor is blinking after <code>abhi</code> then i want that index, so that later after clearing the <code>TextBox</code> programmatically and assigning some other or same text programmatically I want to make the cursor blink just after 4 characters.</p>
  
  <p>I have tried get cursor position like this,</p>
  
  <pre><code>_tempFuncName = txtFunctionName.Text;
  _cursorPosition =  txtFunctionName.SelectionStart;
  _selectionLength = txtFunctionName.SelectionLength;
  </code></pre>
  
  <p>And set back at some later stage from other event like this,</p>
  
  <pre><code>txtFunctionName.Text = _tempFuncName;
  txtFunctionName.SelectionStart = _cursorPosition;
  txtFunctionName.SelectionLength  = _selectionLength;
  </code></pre>
  
  <p>Here underscore variables are page level variables.</p>
  
  <p>This code is not working. Is there some other approach?</p>