Solved

Data Binding in WPF

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
EXECUTE AS Permissions To Query system tables requireing View Server State 10 110
Query performance - SQL Server 20 84
Alternative to visual sourcesafe 2 131
wordpress limitations 4 124
This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 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