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.
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.
Only a smiling visitor here to share the love (:, btw great style .
Hi, Neat post. There’s an issue together with your website in web explorer, would test thisÖ IE nonetheless is the market leader and a huge part of folks will miss your magnificent writing due to this problem.
I was examining some of your blog posts on this website and I conceive this site is really instructive! Keep on putting up.
I like this website so much, saved to bookmarks. “I don’t care what is written about me so long as it isn’t true.” by Dorothy Parker.
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.
I conceive you have remarked some very interesting points , regards for the post.
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.
I really like your writing style, fantastic info , regards for putting up : D.
I’ve recently started a site, the information you offer on this website has helped me greatly. Thanks for all of your time & work.
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.
I am not very good with English but I get hold this very easygoing to translate.
You have noted very interesting points! ps nice website.
I regard something truly special in this internet site.
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.
Keep up the fantastic work , I read few blog posts on this internet site and I conceive that your web blog is real interesting and has bands of fantastic information.
hi!,I really like your writing very much! percentage we be in contact more about your post on AOL? I need an expert on this space to solve my problem. Maybe that’s you! Looking forward to peer you.
As soon as I detected this internet site I went on reddit to share some of the love with them.
Real good info can be found on web site.
Very interesting subject , appreciate it for putting up. “Not by age but by capacity is wisdom acquired.” by Titus Maccius Plautus.
Huge site! I will defently bookmark it!!!
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.
Keep up the excellent work , I read few content on this site and I think that your website is rattling interesting and holds bands of fantastic info .
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.
I always was interested in this topic and still am, regards for putting up.
Some genuinely prime posts on this website , saved to favorites .
hello bros!! gorgeous site!
You have brought up a very good details , thankyou for the post.
I’d must test with you here. Which isn’t one thing I usually do! I enjoy reading a submit that can make people think. Additionally, thanks for allowing me to comment!
Dead written content, Really enjoyed reading through.
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.
I was examining some of your blog posts on this site and I believe this web site is very instructive! Continue putting up.
Some really excellent articles on this website, appreciate it for contribution. “Such evil deeds could religion prompt.” by Lucretius.
I have recently started a web site, the info you offer on this web site has helped me tremendously. Thanks for all of your time & work.
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.
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.
Sweet site, super style and design, very clean and utilise friendly.
Rattling nice layout and excellent content, hardly anything else we require :D.
There is clearly a lot to realize about this. I feel you made certain good points in features also.
You are my inhalation, I possess few web logs and occasionally run out from post :). “The soul that is within me no man can degrade.” by Frederick Douglas.
I am very happy to read this. This is the kind of manual that needs to be given and not the random misinformation that is at the other blogs. Appreciate your sharing this greatest doc.
Thanks for the info, I must bookmark your website for my reference
I come across your website by searching, i just want to say i keep it on my bookmarks, thank you.
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.
I like this blog its a master peace ! Glad I observed this on google. “Don’t spend time beating on a wall, hoping to transform it into a door.” by Dr. Laura Schlessinger.
wonderful submit, very informative. I ponder why the opposite experts of this sector don’t notice this. You must continue your writing. I’m sure, you have a great readers’ base already!
Thank you a lot for sharing this with all of us you really recognize what you are speaking approximately! Bookmarked. Kindly also consult with my website =). We can have a hyperlink alternate agreement among us!
Keep up the wonderful piece of work, I read few posts on this web site and I think that your blog is rattling interesting and has got lots of wonderful info .
Kudos for this article. Highly interesting and excellently penned article. Thanks!
I really enjoy examining on this web site, it has superb articles. “A short saying oft contains much wisdom.” by Sophocles.
Deference to post author, some excellent information . “If you don’t leap, you’ll never know what it’s like to fly.” by Guy Finley.
>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
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!
Amazing! This blog looks exactly like my old one! It’s on a completely different subject but it has pretty much the same page layout and design. Wonderful choice of colors!
Hello There. I found your blog using msn. This is an extremely well written article. I will make sure to bookmark it and return to read more of your useful information.
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.
Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to more added agreeable from you! However, how can we communicate?
I am not very good with English but I find this real easy to interpret.
I was reading through some of your posts on this internet site and I believe this site is rattling instructive! Keep posting.
Very interesting information!Perfect just what I was searching for!
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!
To be honest I haven’ ever seen better blog than this
Straight to the point and well written! Why can’t everyone else be like this?
Thanks in favor of sharing such a pleasant thought, paragraph is good, thats why i have read it fully
I am not rattling great with English but I get hold this very easy to understand.
Very interesting information!Perfect just what I was looking for!
Do you have any video of that? I’d care to find out more details.
Hello there, You’ve performed an incredible job. I’ll definitely digg it and in my view recommend to my friends. I’m sure they will be benefited from this website.
I really enjoy reading on this site, it has got great content . “Those who complain most are most to be complained of.” by Matthew Henry.
Can you please send by e-mail me the code for this script or please let know me in detail in relation to this script?
Enjoyed reading through this, very good stuff, thanks .
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?
I am impressed with this site, very I am a big fan .
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!
As soon as I discovered this web site I went on reddit to share some of the love with them.
Very interesting subject, thank you for putting up.
I view something really interesting about your website so I saved to bookmarks .
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.
I reckon something truly special in this web site.
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!
You have noted very interesting points ! ps decent web site .
I think this WPF Data Binding Options Ovais Mehboob's TechBlog website holds some real superb information for everyone. “It is easy enough to define what the Commonwealth is not. Indeed this is quite a popular pastime.” by Elizabeth II.
Great post, I conceive blog owners should acquire a lot from this website its really user pleasant. So much fantastic info on here :D.
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.
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.
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.
It’s hard to find knowledgeable people on this topic, but you sound like you know what you’re talking about! Thanks
I like this web blog very much so much great information.
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!
This is just what I was looking for. I did not expect that I’d get so much out of reading your write up! You’ve just earned yourself a returning visitor 🙂
There is noticeably a lot to know about this. I assume you made certain nice points in features also.
This internet website could be a walk-through its the data you wanted in regards to this and didn’t know who need to. Glimpse here, and you will completely discover it.
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.
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.
I believe this web site has very superb composed subject matter articles.
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.
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.
Hey. I want to own up this is the intriguing piece of writing. When i me in addition have a website, you are able to it at this point
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!
I just added this feed to my bookmarks. I have to say, I very much enjoy reading your blogs. Thanks!
Some truly prize posts on this website , saved to bookmarks .
Some truly nice and useful information on this web site, as well I believe the pattern holds fantastic features.
Thank you for the write up! Also, I noticed that your RSS feeds aren’t working. Could you take a look at that?
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.
I agree with most of your points, but a few need to be discussed further, I will hold a small talk with my partners and maybe I will look for you some suggestion soon.
Excuse my stupidity, but I can’t seem to find your sites rss feeds. Mind directing me to it? Thanks.
Very nice style and fantastic subject matter, very little else we require : D.
Some really excellent content on this website , regards for contribution.
An interesting post there mate ! Thank you for that .
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!
You have observed very interesting details! ps nice site.
Good site you have here.. It’s difficult to find quality writing like yours these days. I seriously appreciate individuals like you! Take care!!
Pingback: Key practice of MVVM in XAMP apps | Ovais Mehboob's TechBlog