This document assumes that you understand the concepts of object oriented programming and more specifically with C# programming, such as
Events, etc. If not, it will be much harder to follow along.
This tutorial will cover the basics of data
Binding in a WPF application. When you are done with this tutorial, you should be able to create a basic WPF-based graphical program that uses
Binding. We will cover the different types of data
Binding as well as what works and sometimes what doesn’t.
What is data Binding?
The idea of data
Binding is to link a variable of any
object, etc…) to a graphical object’s Property that has the same type.
For example, lets say you have a
Button object called
myButton in your GUI like this: . The words “Click Me!” is a string property in the
Imagine you have a string variable called
strProperty in some part of your code that on its own has no way to interact with your GUI code. Lets say you want to change the
myButton.Text property to match that string variable.
Binding allows the button’s text string to always match a string property in some other object not really related to your GUI so if you change strProperty to equal “Enable” your button text will look like . If you then change the strProperty to “Disable” the button text will automatically change to be without out your back end code having to make any interaction with the GUI on its own.
Binding, you would have to write code yourself that would interact with the GUI and update the
myButton.Text property when ever you update the string in code. In order to do this without
Binding, you would also have to intermingle your background code with your GUI code. This can make it difficult to update or modify your GUI because GUI code is strung throughout all parts of your application. You don’t just have to update your GUI, you have to update all your code that interacts with the GUI.
Binding allows you to have a back end code that is independent of the GUI. This is especially useful when the GUI needs to be updated or improved or when multiple GUIs exists (skins) and you can switch between them.
There are programming styles associated with developing a GUI separate from the back-end. Two of which are Model-View-Control (MVC) or Model-View-ViewModel (MVVM). This tutorial is not going to cover these, however, it is probably wise for you become familiar with these.
However, there is no reason you are limited to
Binding to back end code. You can bind to code that is in the WPF GUI and very powerful applications can be written with little to no back end code.
Requirements for data Binding in WPF
In order to using data Binding, you should have the following requirements:
- Your project should be a WPF Application, or your project should include a WPF Window or WPF Control.
- Objects your elements bind to should implement
Types of data Binding and resources
There is are multiple types of
Binding. Elements bind to some type of resource and there are multiple types of resources. Static and Dynamic resource binding which uses the StaticResource Markup Extension or the DynamicResource Markup Extension.
Binding source can also be “any public property, including properties of other controls, common language runtime (CLR) objects, XAML elements, ADO.NET DataSets, XML Fragments, and so forth.” (Reference: http://msdn.microsoft.com/en-us/magazine/cc163299.aspx).
Go to next: 1.1 Binding one element property to another
Copyright ® Rhyous.com – Linking to this page is allowed without permission and as many as ten lines of this page can be used along with this link. Any other use of this page is allowed only by permission of Rhyous.com.