The Different way of Binding in WPF

No.of Views2018
Bookmarked0 times
Downloads 
Votes0
By  kirtan007   On  25 Jun 2010 09:06:10
Tag : WPF , Data Binding
In this article I will show you about two way binding in WPF application.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this article I will show you about two way binding in WPF application. Its very easy to do in WPF in comparison of windows Application programming.

Two way binding is used when we want to update some controls property when some other related controls property change and when source property change the actual control also updates its property. I think this sentence is hard to understand.

Let me explain you with an example.

Suppose we have one  one Slider control and one textbox. Now if I move slider its value should be displayed in text box and when I input some value in textbox it should be reflected in Slider too.. that's called two way binding.

Implementation

We will create sample application that will demonstrate the same as I mentioned in abstract part of the article.

Step 1

Start with File >> New >>Projects >>Visual C# >> WPF Application

Step 2

DragDrop One Slider control on the WPF form and TextBox

Setup the GUI like shown in below image

Image Loading

Now we want to bind TextBox Text Property to the Slider's value property.In XAML code of TextBox we will write Text Property as below.

Text="{Binding Mode=OneWayToSource, ElementName=Slider1,Path=Value, UpdateSourceTrigger=PropertyChanged}

 Lets understand what it says ,

Mode :  Specify how we want to Bind Control.

here we will have default, OneTime, OneWay ,OneWayToSource, TwoWay.

  • OneTime: changes Slider Property one time only when we write value in Text box
  • OneWay: Only Text box Property will be updated when we move Slider ..but if we write some value in TextBox it will not be reflected in Slider
  • OneWayToSource: by this option we can update the Value of Slider from Text box but can not Update value of textBox from Slider.
  • TwoWay: if we update the value of textbox it will be reflated to Slider and If we update the value of slider it will be reflected in TextBox .

Now when we bind something we need to specify which control we are binding so

ElemenetName : Specify which control to bind ..

Path Specifies which specific property we are trying to bind here we are using Value property becuase we want to Bind Value property of the Slider to TextBox

UpdateSourceTrigger: it specifies when trigger should be fired .. we specified when some PropertyChanged in Source .

Here is My Full XAML Code of Grid take a look.

<Window x:Class="TwowayBinding.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><Slider Name="Slider1" Value="0" Margin="84,85,173,199" SmallChange="1" Maximum="100" ></Slider><TextBox Name="txtValue" Margin="84,118,173,174" Text="{Binding Mode=TwoWay, ElementName=Slider1,Path=Value, UpdateSourceTrigger=PropertyChanged}"></TextBox><Label Content="Value " Height="28" HorizontalAlignment="Left" Margin="39,114,0,0" Name="label1" VerticalAlignment="Top" /><Label Content="Two way binding Demo :" Height="61" HorizontalAlignment="Left" Margin="17,12,0,0" Name="label2" VerticalAlignment="Top" FontSize="32" /></Grid></Window>

 Just write the code like it and Run the project.You have successfully learned how to bind Two Controls Two way .

Conclusion

This article showed you how to work with two way binding in WPF.Thank you for reading.

Sample Project Source

Download source files -50 kb

 
Sign Up to vote for this article
 
About Author
 
kirtan007
Occupation-
Company-
Member Type-Senior
Location-Not Provided
Joined date-02 Jul 2009
Home Page-http://kirtan.uni.cc
Blog Page-
He completed his Bachelor of Computer Application from Gujarat University 2009 .He is doing Master of Computer Application from Gujarat Technological University right now .. His area of Interests are Web Hacking , C# .net Windows form ,asp.net , WPF ,Silverlight ,SQL Server and Some PHP.
 
 
Other popularSectionarticles
Comments
By:fossil relojesDate Of Posted:5/18/2012 2:58:22 AM
Discount Replica D & G Watches, Superior Quality D & G Watches Online
Very informative and useful article indeed. I really like the way writer has presented his views. I hope to see more great articles in future as well. [url=http://www.suizosrelojes.com/fossil-venta-cb643.html]fossil relojes[/url]
By:nike air maxDate Of Posted:11/30/2010 8:34:51 PM
response
First pair of running shoes with Nike Air cushion in 1979 the Tailwind. At the time, this is a huge breakthrough. Nike air max because of reputation in the industry was already quite depressed to bring innovation. But Nike will continue to constantly improve the technology.
By:gg00xivDate Of Posted:9/22/2010 7:13:40 AM
Correction
ElemenetName => ElementName
By:gg00xivDate Of Posted:9/22/2010 7:09:03 AM
Correction
ElemenetName => ElementName
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top