Solved

Data Binding in WPF

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
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…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

19 Experts available now in Live!

Get 1:1 Help Now