Silverlight 3’s New Element Data Binding

One of the improvements you can look forward to in Silverlight 3 is element data binding, also known as element-to-element data binding. Silverlight 2’s {Binding} expression allowed you to specify the name of the property you were binding to, but not the element that owned the property. This typically meant you had to write a line of code to complete the binding. In Silverlight 3, {Binding} expressions can identify elements as well as properties.

Here’s an example:

<Grid x:Name=”LayoutRoot” Background=”White”>

    <Border>

        <Border.Projection>

            <PlaneProjection x:Name=”Projector” />

        </Border.Projection>

        <user:Penguin />

    </Border>

    <StackPanel Orientation=”Vertical” VerticalAlignment=”Top”>

        <Slider Minimum=”0″ Maximum=”360″

            Value=”{Binding RotationX, ElementName=Projector, Mode=TwoWay}”

            Width=”400″ VerticalAlignment=”Top” Margin=”0,20,0,0″ />

        <Slider Minimum=”0″ Maximum=”360″

            Value=”{Binding RotationY, ElementName=Projector, Mode=TwoWay}”

            Width=”400″ VerticalAlignment=”Top” Margin=”0,20,0,0″ />

        <Slider Minimum=”0″ Maximum=”360″

            Value=”{Binding RotationZ, ElementName=Projector, Mode=TwoWay}”

            Width=”400″ VerticalAlignment=”Top” Margin=”0,20,0,0″ />

    </StackPanel>

</Grid>

This XAML declares a user control that draws a penguin, and then wires up a trio of sliders to the RotationX, RotationY, and RotationZ properties of a PlaneProjection. The result? You can rotate the penguin about the X, Y, and Z axes by moving the sliders. And there’s no code required.

Element Data Binding

Pretty cool. But element data binding has a couple of limitations/nuances of which you should be aware. First, I applied the PlaneProjection to a Border containing the penguin user control because element data binding didn’t work when I applied it directly to the user control. I’m assuming this is simply a beta bug and that in the final release, I can get rid of the Border.

Second, and more importantly, the target of an element data binding must be a FrameworkElement derivative. I would have preferred to write my XAML like this:

<Grid x:Name=”LayoutRoot” Background=”White”>

    <Border>

        <Border.Projection>

            <PlaneProjection

                RotationX=”{Binding Value, ElementName=SliderX}”

                RotationY=”{Binding Value, ElementName=SliderY}”

                RotationZ=”{Binding Value, ElementName=SliderZ}”

            />

        </Border.Projection>

        <user:Penguin />

    </Border>

    <StackPanel Orientation=”Vertical” VerticalAlignment=”Top”>

        <Slider x:Name=”SliderX” Minimum=”0″ Maximum=”360″ Value=”0″

            Width=”400″ VerticalAlignment=”Top” Margin=”0,20,0,0″ />

        <Slider x:Name=”SliderY” Minimum=”0″ Maximum=”360″ Value=”0″

            Width=”400″ VerticalAlignment=”Top” Margin=”0,20,0,0″ />

        <Slider x:Name=”SliderZ” Minimum=”0″ Maximum=”360″ Value=”0″

            Width=”400″ VerticalAlignment=”Top” Margin=”0,20,0,0″ />

    </StackPanel>

</Grid>

But that produces a XAML parsing error since PlaneProjection isn’t a FrameworkElement. It is unclear at the moment whether the requirement that an element data binding target a FrameworkElement will change before Silverlight 3 ships. The good news is that lots of elements do derive from FrameworkElement. It is perfectly legal, for example, to bind a Slider representing a volume control to the Volume property of a MediaElement since MediaElement derives from FrameworkElement (and Volume is a dependency property).

Incidentally, I’m always interested to hear about companies who adopt Silverlight and put it to work in creative ways. But when John Robbins told me that Playboy magazine is now using Silverlight Deep Zoom, I wasn’t sure what to think. I wondered why John had suddenly developed an interest in Silverlight. 🙂

I’ve had a blast this week blogging about Silverlight 3, and I have many more tips and samples to share. However, I’m leaving for Microsoft India in a few hours, so I won’t be blogging much for the next week or so. I plan to resume my series on Silverlight 3 the week after next and hope to be posting new entries right up to Devscovery.

Finally, as I wrote the sentence “element data binding has a couple of limitations/nuances of which you should be aware,” I was reminded of a quotation from Winston Churchill:

Churchill

Which is why I sometimes do allow myself to end sentences with prepositions!

2075

The Right Solution for Your Needs.

We deliver a hybrid spectrum of Microsoft Cloud Platform and Azure solutions to government agencies and application developers who demand a modern, open and flexible cloud service platform. We offer trusted, transparent, and secure Infrastructure as a Service (IaaS) and Platform as a Service (PaaS) solutions for production business applications, Business Intelligence (BI), continuous data protection, application availability, test/development, and Software as a Service (SaaS)

Architected to meet your needs.

Deployed flawlessly.

Operated reliably 24x7x365.

Assess:

Rely on our team to map your existing environment to a corresponding 
Azure cloud.

Migrate:

Easily move from your existing environment to a public or private Microsoft cloud.

Re-platform:

Understand how to transform your applications to better take advantage 
of cloud capabilities.

Operate:

Our team actively manages all maintenance & optimization to keep your environment running at its best.

Why Microsoft Cloud Platform and Azure?

Microsoft has made major strides in the public cloud space over the past few years. They are gaining market momentum with companies and also the analysts community who recognize the more than USD15B investment made. At this point Azure has more data centers than AWS and Google Cloud combined. It spans an unparalleled global reach with 36 regions available and more being added. Azure offers companies wanting to put workload in the a public cloud with a secure, scalable and competitive option.

2195

Azure Certified for Hybrid Cloud.

Atmosera has developed core competencies and intellectual property to take full advantage of all Azure has to offer. All Atmosera Azure services receive the “Azure Certified for Hybrid Cloud” designation which confirms it passed Microsoft’s rigorous validation process under the Cloud OS Network (COSN) program. Customers benefit from knowing that their solution was carefully planned and executed using best practices and proven methods.

Stay Informed

Sign up for the latest blogs, events, and insights.

We deliver solutions that accelerate the value of Azure.
Ready to experience the full power of Microsoft Azure?

Atmosera is thrilled to announce that we have been named GitHub AI Partner of the Year.

X