Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
Search :       Advanced Search »
Home » WPF » Data Binding in WPF Controls

Data Binding in WPF Controls

This article shows how to add data binding support among controls in WPF.

Author Rank :
Page Views : 4552
Downloads : 31
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
ControlsDataBinding.zip
 
 
Nevron Chart
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


This article shows how to add data binding support among controls in WPF.  

The Binding Class

The mother of data binding in WPF is the Binding class. Let's take a quick look at how the Binding class glue two objects together to support real-time data transfer.

The Binding class provides the communication channel between two objects regardless of what these objects types. The object that consumes the data is called binding target and the object that provides the data is called binding source. A binding source can be a UIElement, a list object, a CLR object that is associated with ADO.NET data or Web Services, or an XmlNode that contains your XML data.

 

 

 

The data binding process in WPF has five objects - A binding target object, a binding target property, a binding source object, a binding source object property, and a binding object to build a link between these.  The target property must be a dependency property.

 

Data Binding with Controls

Now, using the above approach of the Binding class, we will create an application that will have a real-time data transfer between multiple controls on a WPF page.

We will create an application that looks like Figure 1. In Figure 1, we have a ListBox with a list of colors, a TextBox, and a Canvas. When we pick a color from the ListBox, the text of TextBox and color of Canvas changes dynamically to the color selected in the ListBox and this is possible to do all in XAML without writing a single line of code in the code behind file. 

 

Figure 1.

The XAML code of the page looks like following.

<StackPanel Orientation="Vertical">

    <TextBlock Margin="10,10,10,10" FontWeight="Bold">

        Pick a color from below list

    </TextBlock>

    <ListBox Name="mcListBox" Height="100" Width="100"

             Margin="10,10,0,0" HorizontalAlignment="Left" >

        <ListBoxItem>Orange</ListBoxItem>

        <ListBoxItem>Green</ListBoxItem>

        <ListBoxItem>Blue</ListBoxItem>

        <ListBoxItem>Gray</ListBoxItem>

        <ListBoxItem>LightGray</ListBoxItem>

        <ListBoxItem>Red</ListBoxItem>

    </ListBox>

   <TextBox Height="23" Name="textBox1" Width="120" Margin="10,10,0,0" HorizontalAlignment="Left"  >

        <TextBox.Text>

            <Binding ElementName="mcListBox" Path="SelectedItem.Content"/>

        </TextBox.Text>

    </TextBox>

    <Canvas Margin="10,10,0,0" Height="200" Width="200" HorizontalAlignment="Left" >

        <Canvas.Background>

            <Binding ElementName="mcListBox" Path="SelectedItem.Content"/>

        </Canvas.Background>

    </Canvas>

 

</StackPanel>  

If you look at the TextBox XAML code, you will see the Binding within the TextBox.Text property, which sets the binding from TextBox to another control and another control ID is ElementName and another control's property is Path. So in below code, we are setting the SelectedItem.Content property of ListBox to TextBox.Text property.

        <TextBox.Text>

            <Binding ElementName="mcListBox" Path="SelectedItem.Content"/>

        </TextBox.Text>

 

Now same applies to the Canvas.Background property, where we set it to the SelectedItem.Content of the ListBox. Now, every time you select an item in the ListBox, the TextBox.Text and Canvas.Background properties are set to that selected item in the ListBox.

<Canvas.Background>

    <Binding ElementName="mcListBox" Path="SelectedItem.Content"/>

</Canvas.Background>

 

Now if I pick Red from the ListBox, the output looks like Figure 2, where the color of the canvas is red and the text of TextBox is Red.

Figure 2

Summary

In this article, I discussed how to use the Binding class to provide real-time data transfer among WPF controls.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Praveen Kumar
I have over 13 years of IT industry experience with Microsoft technologies. I hold Masters degree in Computer Science and Applications and Bachelor’s degree in Mathematics. I am responsible for content publishing, product development, and migration of existing contents. I am also responsible for hiring new team members and managing the existing team. I have been awarded Microsoft MVP for the year 2008, 2009, 2010.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
DevExpress Free UI Controls
Become a Sponsor
 Comments
Good Start by Monalisa On December 12, 2008
Hi realy liked ur article and a good one to start learning databinding in WPF. Can u explain more about dynamic binding
Reply | Email | Modify 
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.