Click here to Skip to main content
11,930,788 members (50,276 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


10 bookmarked

A Very Simple Example of Data Binding in WPF

, 4 Feb 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
This is a very simple example of data binding between two controls in WPF.


The importance of data binding in Windows Presentation Foundation, or WPF, application development is undeniable. Yet, most articles are so complex that understanding the basics of this technology can be daunting. This complexity arises from simple beginnings that quickly become complex. This article focuses on data binding between controls, and that's it.


WPF is an important step forward to developing effective graphical user interfaces. Much manual time spent on creating a professional user interface using Windows Forms is addressed by the WPF. Most important of the development tasks in user interface development is the necessary singularity of a data source shared by multiple controls that make up a user interface. This one-to-many sharing of data between source and many controls is called data binding.

What does it do?

As the user types in the textbox control, the data is displayed in a label control right next to it automatically. Because the label control gets its data from the textbox control:


Figure 0: The user interface where the label control on the right gets its data from the textbox control on the left.

Using the code

The code in Figure 2 represents two controls: a textbox and a label, displayed in a Window. When the user types inside the textbox, the text is automatically displayed in the label control, because the label control is data bound to the textbox control. Much of this code is generated when you drag and drop the controls from the Toolbox onto the design area. There is minimal effort to data-bind the two controls together, with the textbox control being the data source.

While the source code in Figure 2 is the complete application code, which you can copy, paste, compile and run, there is only 1 line of code that data-binds the textbox control to the label control:

Content="{Binding ElementName=textBox1,Path=Text}"

Figure 1: 'Content' property of the Label control data bound to the text property of the textbox control.

'ElementName' is a reserved word, as 'Binding' is. So is 'Path'. The curly brackets is part of the XAML syntax. The Path is the textual hierarchical path of an element that makes up the control; similar to the way one would drill down the properties of a control when you right-click on it.

It's possible to use this line inside the definition of any number of controls of an application, and they would all get their data from the textbox control.

Extensible Application Markup Language, or XAML of the MainWindow.xaml file, which is created by Visual Studio 2010 (and probably compatible with other versions as well) is as follows (This is the complete application!):

<Window x:Class="MyWPFdataBinding.MainWindow"
        Title="MainWindow" Height="350" Width="525">
            Content="{Binding ElementName=textBox1,Path=Text}" 
            Width="159" />
            Width="120" />

Figure 2: Complete application source code; but make sure the namespace "MyWPFdataBinding" is also in App.xaml.cs and MainWindow.xaml.cs files.

Points of Interest

Data binding is not limited to between controls, you can also bind to XML data, current DataContext, BindingGroupName, and RelativeSource, all of which you can Google to learn more about. They all follow the general principle discussed here. What makes learning all this challenging is the myriad reserved words associated with the technology, such as "IsSynchronizedWithCurrentItem", "FindAncestor", etc., all of which can be learned through experience. Development time saved using this technology can be enormous.

Next Step

This is as simple an example as I can come up with; let's call it step zero Smile | :) Next level of complexity can be seen in Josh Smith's article:

There is also a helpful CheatSheet for WPF Binding at


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Erol Esen
Software Developer (Senior)
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

Questionthank you Pin
fatema24-Apr-15 6:25
memberfatema24-Apr-15 6:25 
Suggestionvery nice Pin
Member 1129842629-Jan-15 5:06
memberMember 1129842629-Jan-15 5:06 
Suggestionexamples Pin
drussilla24-Jul-14 6:40
memberdrussilla24-Jul-14 6:40 
GeneralRe: examples Pin
Erol Esen24-Jul-14 7:08
memberErol Esen24-Jul-14 7:08 
QuestionData binding Link Pin
abk1015-May-14 2:01
memberabk1015-May-14 2:01 
AnswerRe: Data binding Link Pin
Erol Esen15-May-14 17:26
memberErol Esen15-May-14 17:26 
QuestionThank you. Pin
dhanama22-Oct-13 21:56
memberdhanama22-Oct-13 21:56 
QuestionCan you explain about "Path = Text" Pin
raj23karthik5-Sep-12 0:09
memberraj23karthik5-Sep-12 0:09 
AnswerRe: Can you explain about "Path = Text" Pin
Erol Esen12-Oct-12 5:03
memberErol Esen12-Oct-12 5:03 
GeneralReason for my vote of 5 I'm begginer so I vote 5. Pin
beginner201126-Feb-12 16:50
memberbeginner201126-Feb-12 16:50 
GeneralReason for my vote of 5 It's always useful to explain the ba... Pin
Paul McKIllop31-Jan-12 19:39
memberPaul McKIllop31-Jan-12 19:39 
GeneralReason for my vote of 5 Hey. Please ignore negative comment... Pin
Jaikrishan30-Jan-12 18:46
memberJaikrishan30-Jan-12 18:46 
GeneralReason for my vote of 4 My vote of 4 because it is a simple ... Pin
Member 346973530-Jan-12 18:03
memberMember 346973530-Jan-12 18:03 
GeneralReason for my vote of 1 A tip should intend to do at-least o... Pin
Pankaj Chamria29-Jan-12 5:16
memberPankaj Chamria29-Jan-12 5:16 
QuestionSo we all got down vote for Pin
Lakamraju Raghuram27-Jan-12 7:09
memberLakamraju Raghuram27-Jan-12 7:09 
AnswerRe: So we all got down vote for Pin
Pankaj Chamria29-Jan-12 5:12
memberPankaj Chamria29-Jan-12 5:12 
SuggestionMy Openion Pin
Herbert Yu26-Jan-12 7:14
memberHerbert Yu26-Jan-12 7:14 
GeneralRe: My Opinion Pin
Erol Esen26-Jan-12 8:25
memberErol Esen26-Jan-12 8:25 
QuestionNot an article Pin
Richard MacCutchan26-Jan-12 0:15
mvpRichard MacCutchan26-Jan-12 0:15 
AnswerRe: Not an article Pin
Erol Esen26-Jan-12 2:19
memberErol Esen26-Jan-12 2:19 
GeneralRe: Not an article Pin
Richard MacCutchan26-Jan-12 2:40
mvpRichard MacCutchan26-Jan-12 2:40 
GeneralRe: Not an article Pin
Erol Esen26-Jan-12 2:58
memberErol Esen26-Jan-12 2:58 
GeneralRe: Not an article Pin
Richard MacCutchan26-Jan-12 3:36
mvpRichard MacCutchan26-Jan-12 3:36 
GeneralRe: Not an article Pin
Erol Esen26-Jan-12 4:05
memberErol Esen26-Jan-12 4:05 
GeneralRe: Not an article Pin
Richard MacCutchan26-Jan-12 5:04
mvpRichard MacCutchan26-Jan-12 5:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 4 Feb 2012
Article Copyright 2012 by Erol Esen
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid