WPF Data Binding Options

In this post i will show the different ways of binding WPF control elements with the different sources. WPF provides binding both declarative and imperatively through code. It provides greater flexibility to the developer to bind the target value to source value in the XAML file.

The term data binding describes the process of creating a dependence for the value of one property i.e. the target property to the source property. The target property takes on the value of the source property. In some cases, changes to the source property are transmitted to the target immediately, but in some cases, they are not.

Following are the types of bindings that are the part of this post and mostly used in WPF applications.

1. Binding target value to WPF element.
2. Binding to an Object
3. Binding to Ancestor properties through RelativeSource property.
4. Binding to Data Sources
5. Binding to an Object with ObjectDataProvider

1. Binding Target value to WPF Element:
This type of binding used when you want to bind the target property to any WPF element (control) property. For e.g. you want to bind the Textbox text to the label Content property. When the user write in the textbox it should reflect the change in the label control. In this case, you add a textbox and a label controls in the WPF form. And specify the binding as follows.

Above code snippet shows the binding of Label Content target property with the Textbox Text source property. Now when you run the application and write something on textbox it will directly reflect the changes on the Label.

2. Binding to an Object
When binding to an Object we use Source property to specify the source object. For binding with object, in the Window.Resources tag user has to specify the Resource object. Below code snippet shows the way of binding an Object property with the resource object.

The above code snippet binds the StaticResource to the personObj which was defined in the Window.Resources tag and Path specified the property which holds the value. Now when run the application it will bind the TextBlock text value with the property value. When the form loads it initialize an object and fill the value obtained from that object property. If you want to pull data from some other datasource at runtime then you don’t need to specify the Binding Source property and only have to specify the Path property and in the code behind bind the TextBlock text source to that object value.

You can also specify the StaticResource binding property over Grid level or at container level. So you don’t need to specify the static resource property on every control bindings. All the controls that come under that container will use that datasource. But you need to specify the path which maps the property to that object property. Following snippet shows the way of binding through DataContext property at Container level.

3. Binding to Ancestor properties through RelativeSource property
Using the RelativeSource attribute in the binding you can bind the source to the ancestor property value. For example you have specified the Tag attribute in the Grid container. And you want to bind the Tag property in the Textbox control that comes under grid container. You can do that by using RelativeSource property. Following code snippet shows the way of binding to ancestor properites through RelativeSource property.

4. Binding to Data Sources

This is when you want to bind the Collection controls like Listbox etc. with different datasources.ListBox in WPF contains an ItemSource property which helps to bind the datasource. Normally in business application collection retrieves from database. So you dont need to specify the Source property in the Listbox ItemsSource property and just specify the Binding keyword followed with the DisplayMemberPath which is used to map the object property retrieved from the Collection to the control display item property.

Following code snippet shows the binding.

5. Binding to an Object with ObjectDataProvider

Using ObjectDataProvider class you can bind a WPF element to a method called on an object. You can specify an object type and a method on that type. WPF also provides an option to bind methods that have parameters in its signature and  user can define the method parameters declaratively in XAML with their default values.

Below code snippet shows the way of binding method that contains no parameters in its signature.

In the code above first i created a method named GetText which return some string. And then I have specified the ObjectDataProvider tag and reference it in the TextBlock.

Below code snippet shows the way of binding method that contains two parameters in its signature.

Define method.

Specify ObjectDataProvider in the XAML.

When run the Application and enter the data in two textboxes, the Concat method takes the values from those textboxes and bind the returned concatenated string with the TextBlock as shown below.

112 thoughts on “WPF Data Binding Options

  1. Nice read, I just passed this onto a colleague who was doing some research on that. And he actually bought me lunch since I found it for him smile So let me rephrase that: Thank you for lunch! “By nature, men are nearly alike by practice, they get to be wide apart.” by Confucius.

  2. Greetings I am so grateful I found your weblog, I really found you by mistake, while I was browsing on Digg for something else, Regardless I am here now and would just like to say thanks for a remarkable post and a all round thrilling blog (I also love the theme/design), I donít have time to read through it all at the minute but I have saved it and also added in your RSS feeds, so when I have time I will be back to read much more, Please do keep up the awesome job.

  3. Many thanks for being my personal mentor on this issue. My partner and i enjoyed your own article greatly and most of all enjoyed how you handled the areas I regarded as being controversial. You’re always rather kind towards readers much like me and aid me in my living. Thank you.

  4. I do accept as true with all the ideas you have offered to your post. They are very convincing and will definitely work. Nonetheless, the posts are very brief for newbies. May just you please extend them a little from subsequent time? Thanks for the post.

  5. Appreciating the dedication you put into your web site and in depth details you present. It’s nice to come across your site every once in a while that isn’t the same outdated re-written information. Great study! I’ve bookmarked your internet site and I’m including your RSS feeds to my Google account.

  6. Outstanding post, I think people should acquire a lot from this web site its really user pleasant. “Frisbeetarianism is the belief that when you die, your soul goes up on the roof and gets stuck.” by George Carlin.

  7. Thank you for the sensible critique. Me & my neighbor were just preparing to do a little research on this. We got a grab a book from our area library but I think I learned more from this post. I am very glad to see such wonderful info being shared freely out there.

  8. I would like to thnkx for the efforts you have put in writing this web site. I am hoping the same high-grade blog post from you in the upcoming as well. Actually your creative writing skills has encouraged me to get my own website now. Actually the blogging is spreading its wings quickly. Your write up is a great example of it.

  9. Do you have a spam problem on this blog; I also am a blogger, and I was wondering your situation; many of us have created some nice methods and we are looking to exchange methods with other folks, why not shoot me an e-mail if interested.

  10. Attractive section of content. I just stumbled upon your blog and in accession capital to assert that I acquire in fact enjoyed account your blog posts. Anyway I will be subscribing to your feeds and even I achievement you access consistently quickly.

  11. >Fantastic publish, Im attempting to get as significantly details as feasible at the moment for my final project to guarantee I get the finest results achievable, so hold it coming

  12. What i do not understood is in reality how you are now not actually a lot more well-liked than you might be right now. You’re so intelligent. You recognize therefore significantly in the case of this subject, made me in my opinion imagine it from so many various angles. Its like women and men aren’t interested except it’s one thing to accomplish with Lady gaga! Your individual stuffs outstanding. All the time care for it up!

  13. The next time I read a weblog, I hope that it doesnt disappoint me as a lot as this one. I imply, I know it was my option to learn, however I really thought youd have something fascinating to say. All I hear is a bunch of whining about something that you could possibly repair if you happen to werent too busy searching for attention.

  14. Pretty nice post. I just stumbled upon your weblog and wanted to say that I’ve really enjoyed surfing around your blog posts. In any case I will be subscribing to your feed and I hope you write again soon!

  15. Thank you, I have recently been searching for information approximately this topic for a long time and yours is the best I’ve found out till now. However, what about the conclusion? Are you certain about the source?

  16. Howdy! I know this is kinda off topic nevertheless I’d figured I’d ask. Would you be interested in trading links or maybe guest authoring a blog post or vice-versa? My site discusses a lot of the same topics as yours and I think we could greatly benefit from each other. If you happen to be interested feel free to send me an e-mail. I look forward to hearing from you! Wonderful blog by the way!

  17. Pretty section of content. I just stumbled upon your website and in accession capital to assert that I acquire actually enjoyed account your blog posts. Anyway I’ll be subscribing to your feeds and even I achievement you access consistently quickly.

  18. My neighbor and I have been just debating this particular topic, he’s often seeking to show me incorrect. Your view on that is nice and exactly how I really feel. I just now mailed him this site to point out him your personal view. After trying over your web site I book marked and might be coming back to learn your new posts!

  19. Thank you for the wise critique. Me & my neighbour had been making ready to do some research about that. We obtained a superb guide on that matter from our native library and most books where not as influensive as your information. I am very glad to see such information which I was searching for a protracted time.This made very glad! Anyway, in my language, there are not much good source like this.

  20. I would also like to add if you do not now have an insurance policy or maybe you do not take part in any group insurance, you could possibly well gain from seeking assistance from a health insurance agent. Self-employed or people with medical conditions normally seek the help of one health insurance agent. Thanks for your text.

  21. i know i’m a little off topic, but i just wanted to say i love the layout of your blog. i’m new to the blogegine platform, so any suggestions on getting my blog looking nice would be appreciated.

  22. My partner and I absolutely love your blog and find most of your post’s to be exactly I’m looking for. can you offer guest writers to write content for you? I wouldn’t mind creating a post or elaborating on a lot of the subjects you write concerning here. Again, awesome blog!

  23. I’m honored to obtain a call from my friend as he found out the important suggestions shared on your site. Examining your blog posting is a real brilliant experience. Thanks again for thinking of readers like me, and I desire for you the best of success as a professional surface area.

  24. Aw, this was a really nice post. In idea I would like to put in writing like this additionally – taking time and actual effort to make a very good article… but what can I say… I procrastinate alot and by no means seem to get something done.

  25. Thank you for the sensible critique. Me & my neighbor were just preparing to do a little research about this. We got a grab a book from our local library but I think I learned more from this post. I’m very glad to see such great information being shared freely out there.

  26. I like this web site very much, Its a rattling nice berth to read and incur info. “Intimacy is what makes a marriage, not a ceremony, not a piece of paper from the state.” by Kathleen Norris.

  27. Actually great submit, Thank you for sharing This knowledge.Excellently drafted article, if only all bloggers offered similar level of content while you, the internet would be a very much better place. Please retain it up!

  28. Thank you for your complete valuable efforts on this blog. My mother takes pleasure in under-going investigations plus its simple to grasp why. The majority of us hear all with regards to the powerful means you convey insightful information as a result of the internet site and as well improve contribution from guests on that content plus my simple princess is undoubtedly learning a great deal. Take pleasure in the remaining area of the new year. You’re doing a very good job.

  29. I think other web site proprietors should take this web site as an model, very clean and excellent user genial style and design, as well as the content. You’re an expert in this topic!

  30. Pingback: Key practice of MVVM in XAMP apps | Ovais Mehboob's TechBlog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s