Thursday, February 26, 2009

Text selection in WPF TextBox

There are so many methods and properties which deals with selection in WPF TextBox.
Name Description
TextBox.Select() Accepts initial position and length of selection.
TextBox.SelectAll() Selects full text.
TextBox.SelectionStart Direct property which tells the start of selection.
TextBox.SelectionLength Direct property which tells the length of selection.
TextBox.SelectedText Gives the currently selected text.

But if you just use these methods and properties the selection will not come as expected.ie the text won't get selected even if we set these properties or call methods.

The reason for this is very simple.The TextBox is not in focus.So make sure that the TextBox is in focus before using programmatic selection related members.Or give Focus to the TextBox before selecting.

tbResult.Focus();

tbResult.SelectionStart = tbResult.Text.IndexOf("ControlTemplate TargetType=");
tbResult.SelectionLength = 230;

Wednesday, February 25, 2009

Limitations of XamlWriter.Save method

XamlWriter.Save method is very much useful if we need to preserve the state of an application.But when I used this method extensively, I came to know some limitations.Here is the list.

  1. From Microsoft http://msdn.microsoft.com/en-us/library/ms754193.aspx
  2. We can't save Style of a control which contains VisualStateManger nodes ie we will loose vsm support if we save using XamlWriter.Save method.

Will be extending whenever I find more.