Solved

Data Binding in WPF

Posted on 2010-08-23
3
319 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…

733 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