Most of us like fancy UI effects or animations but none of this is possible without the ability to DataBind.

So before we run, we must learn to walk. Let’s jump into the shallow end and start a fresh project for our example. We will need a Slider, TexBlock and Rectangle Control. We will now need to give each Control a Name Property. We will also need to set the Maximum value of the Slider Control to 180.

Also what we need to do is create the RenderTransform Property of the Rectangle Control. We will now call upon the RotateTransform Property of the RenderTransform, set the Name, as well as the Angle.

Now we will be doing simple DataBinding by setting the Value of the equal the Binding of the ElementName of the RotateTransform Property and Path to the Angle. What we have done here is make a connection between the Slider to the Rectangle.

But if you try to run the application now we get an error. The issue is that the Mode property of the Binding Statement of the Slider is in its default setting, which is OneWay.

What this means is that data is being sent from the Angle, which gives us the starting point for our Slider. To also allow data to be sent back to the source and have an effect as well we will need to send the Mode to TwoWay.

Run the application now to see how the user input on the Slider is a constant changing value that is “Bound” to our Rectangle Control. This has all been taken care of in our XAML page. To DataBind a Control Property programmatically, we will need to do so right after the InitializeComponent Method is called.

First we will need to create an instance of the Binding class, which will contain the ElementName, as well as Path to bind to. We will then call the Control that we wish to bind to the data and set it to a property of the Control. We must also make reference to the Binding class object that we wish to bind to.

As you can see, I have set the TextProperty of the TextBlock Control equal to the Value of the Slider Control.

This will now draw this lesson to “el fin.” If you have any other questions or concerns and have not yet downloaded the Source Code for this project, I strongly suggest doing so. Seeing it in its entirety can help clear up many of your questions. Thank you for your time and I hope this has been helpful, take care.

Download Source Files