Welcome to irritatedVowel.com Sign in | Help

POKE 53280,0: Pete Brown's Blog

Silverlight, WPF, Windows Client Development, Woodworking, .NET Programming, CNC, Nature, and other topics.

A former Microsoft Silverlight MVP and INETA Speaker, Pete Brown writes on a number of topics including Silverlight, WPF, .NET, woodworking and working as a consultant in the DC area. read more

Subscribe

Subscribe to my feed
Add to Technorati Favorites

Community Events


who's online

AddThis Social Bookmark Button

From Silverlight in Action 2nd Edition: UI Element Binding

One reason I’ve been so quiet over the past couple months, is I have been scrambling to update Chad Campbell and John Stockton’s excellent Manning Silverlight 2 in Action book to Silverlight 3, under the title Silverlight in Action 2nd Edition (the Amazon info is out of date), to be released this fall.

I’ve done a little restructuring (but not a ton) in the process, and have also been covering as many new Silverlight 3 goodies as I can, as well as a couple extras like ViewModel and .NET RIA Services.

I just finished writing the section about UI Element binding in Silverlight 3, and thought I’d share some of the pre-edited version with you all, since the example here solves something I’m often asked to do in Silverlight applications. Enjoy!

As a reminder, Manning Early Access Program (MEAP) members will be getting chapters of the book in the next couple days, with a much larger release of content just after Silverlight 3 goes live. If you’re looking to get an edge up on Silverlight 3, I’d definitely recommend joining MEAP.

7.2.3 Binding to a UI element

Binding one or more properties of a UI Element to values on an entity, ViewModel or business object is certainly a compelling use of binding. Sometimes, though, you want to use binding for things we wouldn't traditionally consider "data", things within the user interface. You may want to bind the height of two controls together so that they resize equally, or perhaps you want to bind three sliders to the x, y, and z-axis rotations respectively of a plane (see chapter n for more information on 3d rotation in Silverlight). Rather than binding to gather input or display data to the user, you're binding to avoid writing extra plumbing code.

Let's say that you want to display a count of characters entered into a TextBox in real-time. Something like the figure below.

clip_image002

You could certainly do that in code, but that would be fairly uninteresting code to write, and would need to refer to XAML elements by name, or have event handlers wired in XAML, introducing a dependency on the specific page's code-behind and making it less portable and potentially more brittle. In addition, you'd find yourself doing it enough that you'd either wrap the TextBox in your own CountingCharsTextBox control, or add a helper buddy class or something.

Or, if you prefer a XAML approach, which I hope I've sold you on by now, Silverlight 3 introduced the concept of Element Binding. Element Binding allows you to bind the properties of one FrameworkElement to another FrameworkElement. The usual restrictions apply (target must be a DependencyProperty, source must notify of changes) so you cannot using element binding quite everywhere.

To produce the above TextBox with the automatic count of characters using Element Binding, the markup is pretty straight-forward and entirely self-contained:

<StackPanel Orientation="Vertical" Margin="50">
  <TextBlock Text="Tweet (max 140 characters)" />
  <TextBox x:Name="tweetText"
           MaxLength="140"
           Text="Right now I'm writing a book" />

  <StackPanel Orientation="Horizontal">
    <TextBlock Text="{Binding Text.Length, ElementName=tweetText}" />
    <TextBlock Text="/" />
    <TextBlock Text="{Binding MaxLength, ElementName=tweetText}" />
  </StackPanel>

</StackPanel>

The above XAML will show a TextBox with a count of characters underneath it. The character count will update in real-time to show the number of characters typed into the TextBox. Note also that the MaxLength displayed under the textbox is actually coming from the TextBox itself (the 140 in the label is not, however). The key item that makes this happen is the ElementName parameter in the binding expression. ElementName is, as it suggests, the name of another element on the XAML page.

  Add to Technorati Favorites
Posted: Wednesday, June 24, 2009 2:18 AM by Pete.Brown

Comments

Davide Zordan said:

Cool, as usually I'll join MEAP :)
# June 24, 2009 5:58 AM

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout
# June 25, 2009 10:36 AM

Jason N. Gaylord's Blog said:

If you are looking to follow this series, be sure to subscribe to my RSS feed at http://feeds.jasongaylord
# June 25, 2009 4:53 PM

Community Blogs said:

If you are looking to follow this series, be sure to subscribe to my RSS feed at http://feeds.jasongaylord
# June 25, 2009 4:55 PM

ASPInsiders said:

If you are looking to follow this series, be sure to subscribe to my RSS feed at http://feeds.jasongaylord.com/JasonNGaylord
# June 25, 2009 6:01 PM

austin avrashow said:

Manning emailed a good offer to 1st edition MEAP customers. $9.99 for the MEAP 2nd edition or $19.99 for that and the printed book. Valid for only a week or so.
# June 28, 2009 6:19 AM

Zunanji viri said:

Binding to elements is yet another feature, known to WPF-ers since v1. Silverlight 3 now allows bindings
# June 29, 2009 4:41 PM

torxh@qq.com said:

I found the topic "SharePoint 2003 Image Viewer Webpart written in Silverlight",and In my mind the sps2003 is based on dotnet1.1 but the silverlight must be created with vs2008. I want to know how and wish for your help . can you send me the project file? thanks for you! my contract torxh@qq.com.
# July 9, 2009 5:11 AM

Pete.Brown said:

@torx The code for the Silverlight application would be in a completely separate solution from the SharePoint code. If you're really working with sps2003 and .NET 1.1 (VS 2003) My guess is you'd need to do the work on separate machines or in a virtual machine. Questions like this usually do best on the forums at http://silverlight.net Pete
# July 10, 2009 5:45 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Enter the text you see in the image:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS