As I was coding on my app for Windows Phone 7 I ran in to an issue and found no reference to it on the web, so I thought I would post about it.

I was making a test page to show what’s in the Isolated Storage, to verify that my writes was working as they should. When writing this test page I used a textbox and wrote some results too. I wanted to use the SelectionChanged event to catch the selection but I could not get it to work.

After scouring the web I couldn’t find any one with similar issues except people posting about a bug that was firing off two events instead of one for the SelectionChanged event. I could debug and see that the event really fired. But I could not get it to return any value other than an empty string.
The code used was a simple:

private void textBox2_SelectionChanged(object sender, RoutedEventArgs e)
{
    textBox1.Text = textBox2.SelectedText;
}

Since I only wanted to select items from this textbox I had set the property IsReadOnly to true. And it seems there is a bug in the beta tools, because when setting this property to false the event returns the proper value. So, using a textbox which is editable will enable you to receive the proper value. The screenshot below shows the event having returned the proper value after the textbox control has been marked as editable.

Now I know this isn’t proper usage of a textbox, showing static texts and making them selectable should be handled by a listbox, but some one still might try this out so the information could be pertinent.

WP7 Dev – Problems with TextBox SelectionChanged event
Tagged on:     

One thought on “WP7 Dev – Problems with TextBox SelectionChanged event

  • February 23, 2011 at 20:20
    Permalink

    I have been searching for info about this behavior for days. thanks for having nice and useful post

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.