Windows Phone 7 – No “editable” TextBlock

October 16, 2010 5:11 pm

As I’m diving into Windows Phone 7 development and making notes for myself on how WP7 compares to WebOS, I’ve come across one little wrinkle that works really nicely in WebOS (out-of-the-box) and doesn’t work so well in WP7 (out-of-the-box).

I’m talking about a control (actually, a Widget in WebOS) that initially looks like a regular text label, but when you tap on it, it turns into a text box that lets you edit the contained text. WP7 does not have anything like this out-of-the-box. So I decided to create my own.

I made a UserControl that consists of a TextBlock and a TextBox. The TextBox is normally Collapsed (Hidden doesn’t exist on WP7, you’d have to use Opacity=”0″ instead). When the user taps on the TextBlock, it is collapsed and the TextBox is made visible. Once the TextBox loses focus, the reverse happens, and the text from the TextBox is transferred to the TextBlock. Since it can be useful to be able to style the TextBlock and to provide InputScope, I’ve also added a few DependencyProperties to enable that. The code is a little “smelly”, perhaps, because it could be refactored into a proper CustomControl, but what I have so far works well enough for me.

If you want to use it or just have a look, feel free to download the source code for TextBlockEditable.

6 Responses to “Windows Phone 7 – No “editable” TextBlock”

Josh Einstein wrote a comment on October 16, 2010

I don’t get it. What’s the difference between that and a text box that has a transparent border when it doesn’t have focus?

gius wrote a comment on October 17, 2010

Hi, what about a textbox styled that without focus, it would look like a textblock?

GeekTieGuy wrote a comment on October 17, 2010

Chalk it up to my inexperience with Silverlight.

wekempf wrote a comment on October 19, 2010

The number one rule in WPF and Silverlight when it comes to controls is to first consider applying a template or style to an existing control before considering creating a completely new control. It’s actually quite astounding how many controls are custom controls in other UI frameworks that can be rendered in WPF/Silverlight using one of the standard controls and some template/style changes.

GeekTieGuy wrote a comment on October 19, 2010

I quite agree. And my history is with WPF. I didn’t have the patience to dig into the differences between the Phone Silverlight controls and the Desktop WPF ones. The little digging I did left me a little disappointed at how much of WPF has been forced out of Silverlight in order to make it easy to make cross-platform/small and lean. I looked at the control template for TextBox on the Phone (using Blend) and, to be completely honest, I didn’t fully understand how it works so I gave up trying to make it do what I wanted. I saw a quicker return on my investment of time and effort by “brute forcing”. You know how deadlines can be a forcing function.

[...] into an edit box, where you can change the content. I liked this control so much, I just had to write my own version of it. The Silverlight limitations on WP7 (I can’t remember at this late stage if it was lack [...]

Care to comment?

%d bloggers like this: