Click here to Skip to main content

A Very Simple Example of Data Binding in WPF

This is a very simple example of data binding between two controls in WPF.
Sign Up to vote bad good
Add a reason or comment to your vote: x
Votes of 3 or less require a comment
See more: C#WPF

Introduction

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.

Background

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:

ScreenHunter_04_Jan._27_08.03.gif

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"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Label 
            Content="{Binding ElementName=textBox1,Path=Text}" 
            Height="29" 
            HorizontalAlignment="Left" 
            Margin="292,28,0,0" 
            Name="label1" 
            VerticalAlignment="Top" 
            Width="159" />
        <TextBox 
            Height="23" 
            HorizontalAlignment="Left" 
            Margin="41,30,0,0" 
            Name="textBox1" 
            VerticalAlignment="Top" 
            Width="120" />
    </Grid>
</Window>

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: http://www.codeproject.com/Articles/26210/Moving-Toward-WPF-Data-Binding-One-Step-at-a-Time

There is also a helpful CheatSheet for WPF Binding at http://www.nbdtech.com/Free/WpfBinding.pdf

Posted 27 Jan '12
Erol Esen1.6K
Edited 4 Feb '12

Comments
Pankaj Chamria - 29 Jan '12
Reason for my vote of 1 A tip should intend to do at-least one of the following - 1. Solve a complex problem using a brilliant idea. 2. Give a simpler solution to a common problem, where your solution is either better performing/ saves development time/ is more maintainable 3. A decent workaround to a problem to which there is no permamnent solution. Ex: Workarounds to overcome limitations of a framework. Your tip I am afraid is none of these.. Its simply a small text book tutorial for beginners. I maintain that it is neither an article nor a tip.
Member 3469735 - 30 Jan '12
Reason for my vote of 4 My vote of 4 because it is a simple introduction and isn't exciting to everyone. However, I think it is a great mini-article because sometimes it is REALLY hard to find a simple explanation of what seems to be obvious to people who have forgotten what it is like to be starting out at anything. NICE work! Ignore the negative comments, people starting out need stuff like this.
Jaikrishan - 30 Jan '12
Reason for my vote of 5 Hey. Please ignore negative comments. It is great you share something even it is simple. Thanks for your article.
Paul McKIllop - 1 Feb '12
Reason for my vote of 5 It's always useful to explain the basics.

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

Your Filters
Interested
Ignored
     
  1. SAKryukov (583)
  2. CRDave1988 (390)
  3. CPallini (315)
  4. Varun Sareen (308)
  1. SAKryukov (13,971)
  2. OriginalGriff (8,238)
  3. Christian Graus (7,396)
  4. thatraja (5,300)
  5. Abhinav S (5,103)

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionSo we all got down vote for PinmemberLakamraju Raghuram7:09 27 Jan '12  
AnswerRe: So we all got down vote for PinmemberPankaj Chamria5:12 29 Jan '12  
SuggestionMy Openion PinmemberHerbert Yu7:14 26 Jan '12  
GeneralRe: My Opinion PinmemberErol Esen8:25 26 Jan '12  
QuestionNot an article PinmvpRichard MacCutchan0:15 26 Jan '12  
AnswerRe: Not an article PinmemberErol Esen2:19 26 Jan '12  
GeneralRe: Not an article PinmvpRichard MacCutchan2:40 26 Jan '12  
GeneralRe: Not an article PinmemberErol Esen2:58 26 Jan '12  
GeneralRe: Not an article PinmvpRichard MacCutchan3:36 26 Jan '12  
GeneralRe: Not an article PinmemberErol Esen4:05 26 Jan '12  
GeneralRe: Not an article PinmvpRichard MacCutchan5:04 26 Jan '12  
GeneralRe: Not an article PinmemberErol Esen5:17 26 Jan '12  
SuggestionLess for an Article [modified] PinmemberLakamraju Raghuram7:00 25 Jan '12  
GeneralRe: Less for an Article PinmemberErol Esen2:19 26 Jan '12  
GeneralRe: Less for an Article PinmvpDave Kreskowiak2:41 26 Jan '12  
GeneralRe: Less for an Article PinmemberErol Esen3:02 26 Jan '12  
GeneralRe: Less for an Article PinmvpDave Kreskowiak4:40 26 Jan '12  
GeneralRe: Less for an Article PinmemberPankaj Chamria4:59 27 Jan '12  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.5.120222.1 | Last Updated 4 Feb 2012
Copyright © CodeProject, 1999-2012
All Rights Reserved. Terms of Use
Layout: fixed | fluid