Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Data Binding in WPF

Posted on 2010-08-23
3
Medium Priority
?
327 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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
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…
Loops Section Overview
Suggested Courses

810 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