Solved

Data Binding in WPF

Posted on 2010-08-23
3
317 Views
Last Modified: 2013-11-12
Coming from a Flex background I am curious to know if its possible to data bind to local objects in memory in WPF.

The main reason for this is for implementing Model View Controller, where say inside of a WPF Control ill have a reference to the Model and on the actual WPF control certain visual elements properties need to be databound to properties inside the reference model object.  Is this possible in WPF?  The Microsoft tutorials  are not very clear about how to go about such a basic task, in Flex its simple, if it was a text box for example

text={model.textField}

and thats it! but wpf seems alot more complicated and I have not seen an example of direct static reference binding
0
Comment
Question by:recruitit
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
Gramatan earned 500 total points
ID: 33502695
in  WPF you could use text="{Binding myField}" where myField is a public property of the object  containg the data you want mapped.  
You can set the Model or object  by using the DataContext property in your c# (or vb) code ie; MyXamlForm.DataContext = MyModel
Once that is set MyXamlForm will be able to bind to any public properties in your object.

Also - the public property you map to should also be a Dependency Property. You can use the Visual Studio code snippet (propd) to set up the syntax and save you from typing all the code that goes with that. This also means your class should implement the DependencyObject class.
0
 

Author Closing Comment

by:recruitit
ID: 33502780
think ill stick to flex hehe :)
0
 
LVL 1

Expert Comment

by:Gramatan
ID: 33503398
Thanks for the credit.
I'm afraid I made the answer look more confusing than it is. In its simplest form you just bind properties in the wpf form to a public property in your class/object. From your flex example it looked very similar to me.

The Xaml looks like:
<TextBlock Text="{Binding weekday}" Margin="2,0" />

You can also bind to arrays, collections and lists. it's actually very flexable. no pun intended.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now