Blog - Microsoft .NET, ASP.NET, AJAX and more

Silverlight 3 ElementName DataBinding Problem

by Damien White 11/19/2009 10:27:09 AM

I’ve been using Silverlight 3 heavily for the past few months, and during that time have discovered many things to love and hate.  One problem I faced with Silverlight 3 (that I should have blogged about earlier) is something I think many developers may have faced.  In the application I’ve been working on, we’ve broken up our functionality into “partial views” (we’re using MVVM) by encapsulating logic into Silverlight UserControls.  Well with this approach, we ran into an interesting binding problem.

Let’s say you have a UserControl, and that UserControl exposes a DependencyProperty.  Then within the UserControls content, you try to bind a control to that Dependency Property.  Well there’s an interesting issue with that approach in Silverlight 3.  Let’s look at a very simple example and see where the problem exhibits itself.

In this example, we’ll be creating a totally useless application to illustrate the point.  We’ll have a Page (MainPage.xaml), and one UserControl (MyControl.xaml).  The UserControl will consist of a single string property called MyText and the goal is to get that property’s value to be displayed in a TextBlock (which is within the UserControl).  We’ll then add that UserControl to the page, and we’ll dynamically set MyText on the UserControl.  Simple setup, right?  What can go wrong? Well, let’s get started.

More...

Shout it kick it on DotNetKicks.com Bookmark and Share
Tags: ,
Categories: Silverlight
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed