Content by Category
.NET 1.x
.NET 2.0
.NET 3.0
.NET 3.5
.NET 4.0
.NET 4.5
.NET Assemblies
.NET Framework
.NET Getting Started
Accessibility
ADO.NET
Advertorials
Agile Development
AJAX
Amazon Web Services
Analysis Services
Android
Architecture
Arduino
ASP .NET Web API
ASP.NET
ASP.NET MVC
ASP.NET WebForms
Azure
B2B (Business Integration)
BDD
Big Data
Bing
BizTalk
Book Excerpts
Build and Deploy
Business Intelligence
C#
C++
ClickOnce
Cloud Computing
Code Contracts
CODE Framework Info - non Technical
CODE on the Road!
COM+
Community
Conferences
Continuous Integration
Crystal Reports
CSLA.NET
CSS
Data
Debugger
Design Patterns
Development Process
Display Technologies
Distributed Computing
Document Database
DotNetNuke
DSL
Dynamic Languages
Dynamic Programming
Editorials
Enterprise Services ("COM+")
Entity Framework
Events
Expression Blend
F#
Fox to Fox
Frameworks
Functional Programming
Git
Graphics
HTML 5
Internet Explorer 8.0
Interviews
IOS
iPhone
Iron Ruby
Java
Java Script
JavaScript
jQuery
JSON
Lightswitch
LINQ
Linux
LUA
Mac OS X
MDX
Messaging
Metro
Microsoft Application Blocks
Microsoft Business Rules Framework
Microsoft Dynamics
Microsoft Expression
Microsoft Office
Mobile Development
Mobile PC
Mono
MsBuild
MVVM
MySQL
Network
NHibernate
node.js
NOSQL
Nuget
Object Oriented Development
Objective C
Odata
OLAP
Open Source
Opinion
Opinions
Oracle
ORM
Other Languages
Parallel Programming
Patterns
PHP
Podcasts
Post Mortem
PowerPoint
Print/Output
Prism
Product News
Product Reviews
Project Management
Prolog
Python
Q&A
Rails
Rake
Razor
Reporting Services
REST
RIA Services
Ruby
Ruby on Rails
Scheme
Search
Security
Services
SharePoint
SignalR
Silverlight
SOA
Social Networks
Software & Law
Software Business
Source Control
Speech-Enabled Applications
SQL Server
SQL Server 2000
SQL Server 2005
SQL Server 2008
SQL Server 2012
SQL Server CE/AnyWhere/Mobile/Compact
SSIS
Subversion
Sync Framework
Tablet PC
TDD
Team System
Techniques
Testing and Quality Control
TFS
Tips
TypeScript
UI Design
UML
User Groups
VB Script
VB.NET
Version Control
VFP and .NET
VFP and SQL Server
Virtual Earth
Vista
Visual Basic
Visual Basic 6 (and older)
Visual FoxPro
Visual Studio .NET
Visual Studio 11
Visual Studio 2005
Visual Studio 2008
Visual Studio 2010
Visual Studio 2011
Visual Studio 2012
Visual Studio Tools for Office
VSX
WCF
Web Development (general)
Web Services
WebMatrix
WF
Whitepapers
Windows 7
Windows 8
Windows Azure
Windows Live
Windows Phone 7
Windows Phone SDK
Windows Server
Windows Vista
WinForms
WinRT
Workflow
WPF
XAML
Xiine Documentation
XML
XNA
XSLT



LearnNow


XAMALOT
 


SSWUG


Reader rating:
Click here to read 8 comments about this article.
Article source: CoDe (2008 Nov/Dec)


Article Pages:  1  2 - Next >


Flexible and Powerful Data Binding with WPF

All applications are dependent on data in some form and most developers find themselves writing reams of data access code. Microsoft has been building data binding frameworks for years. Each one promises to solve our data binding woes forever. We're still waiting for the perfect one. Is WPF data binding the one we've been waiting for?

Windows Presentation Foundation (WPF) is full of surprises for seasoned .NET developers. There is a new UI composition model, a brand new XAML markup language, and the perplexing dependency property system to learn. There is so much to learn, where do you start? For starters, check out Wei-Meng Lee's “An Overview of Windows Presentation Foundation” article (CoDe Magazine, Mar/Apr 2006). I have my own short list of favorite concepts to learn in WPF. Here are two. First, learn about Templates and their close cousin Styles. They are an astoundingly good way to design and render a user interface. Second, invest some time in understanding the new data binding model. It's time well spent

Binding Frameworks

Data binding has been around in some form for years. Both Windows Forms and ASP.NET have binding implementations available. The motivation behind creating a binding framework is that it reduces the amount of code that the application developer must write. You rely on the teams at Microsoft to produce the mountains of code necessary to simplify your daily coding tasks.

"
WPF is a complete rethinking of how to construct a UI development platform. Since Microsoft started with a blank slate, it provided the WPF architects with the opportunity to engineer interesting ideas into their binding engine.
"

The central idea behind binding is simple. You “bind” some UI controls to a data object and rely on the binding framework to push the data to the controls and ensure that changed data is saved back to the business object properties.

So what is WPF data binding anyway? I'd define it as follows: the ability to register two items, a data source and a data target, with the binding framework. The binding framework is responsible for synchronizing the data between the two items and providing us with indispensable services like validation and data conversion.

That simple explanation hardly reveals the power of WPF binding. WPF is a complete rethinking of how to construct a UI development platform. Since Microsoft started with a blank slate, it provided the WPF architects with the opportunity to engineer interesting ideas into their binding engine. Binding in WPF is pervasive and built-in to every corner of the system. It permits better abstraction of code and UI by allowing complete separation of UI design from business object code. Data templates are another unique idea within WPF. If you are coming from a Windows Forms or ASP.NET background, templates will force you to change the way you think about developing and designing user interfaces.

Data Binding Collaborators

There are three main participants in the WPF data binding model; the framework, the data target, and the data source. Configuring a binding with the framework is simply a matter of telling the WPF dependency property system the details about the desired binding. You do this via the Binding markup extension class:

<TextBlock Text='{Binding Path=StreetAddress}' />

This short XAML (pronounced "zammel") snippet is all you need to bind the Text property to the StreetAddress property.

Data Targets

Bindings are always set on the “binding target”. The target is usually a WPF UI element, though there is no requirement for it to be a visual element.

The data target is the destination for the bound data. You have to specify both the target object and target property. The target property must be a dependency property. Dependency properties are a core part of the WPF dependency property system. They are the enablers for many of the exciting WPF features like animation, styles, templates, and property inheritance. They also provide the glue for data binding.

Any type can serve as a data target as long as it provides one or more “dependency properties”. To find out which properties are eligible, here's a simple LINQ query that returns all the dependency properties in the TextBlock class.

In C#:

// using System.Windows.Controls
var q = from member
  in typeof(TextBlock).GetFields
   (BindingFlags.Public | BindingFlags.Static |
    BindingFlags.FlattenHierarchy)
  where member.FieldType ==  
    typeof(System.Windows.DependencyProperty)
  orderby member.Name
  select member.Name;

  // Examples
  // BackgroundProperty
  // CursorProperty

In Visual Basic:

  ' using System.Windows.Controls
  Dim q = From member In 
GetType(TextBlock).GetFields(BindingFlags.Public _
   Or BindingFlags.Static _
   Or BindingFlags.FlattenHierarchy) _
   Where member.FieldType Is GetType(System.Windows.DependencyProperty) _
   Order By member.Name _
   Select member.Name

  ' Examples
  ' BackgroundProperty
  ' CursorProperty

If you run the “List of Dependency Properties” item in the included sample project you can see a detailed list of available dependency properties for each Framework element.

There are a variety of WPF elements that can serve as data targets. Some of these elements are designed for showing single values. The TextBlock and Slider elements are prime examples of this type of element. ItemsControls are elements that show lists of data. WPF includes a number of these list-friendly controls including ComboBox, ListBox, and TreeView.

Data Sources

One of the splendid aspects of the WPF data binding engine is that it can bind to nearly any data source including XML data, ADO.NET classes, LINQ queries, WPF elements, and types that implement IEnumerable or IList.

As has been discussed earlier, every binding must have a binding target and target property. The binding isn't complete unless you also specify the data source and source property.

Sometimes you want to show a single value from a data source. It might be showing a font name in a label or an invoice date in a calendar control. Perhaps you'd like to bind the value of one control to the input of another. I'll call this “single property” binding though there is no such official name in WPF. Single property binding is easy to do in WPF. Here is an example showing how to bind the Value property of a Slider control to the FontSize property of a TextBlock:

<Slider x:Name='sizeSlider'
        Orientation='Vertical'
        Minimum='10'
        Maximum='80'
        Value='25' />
<TextBlock Text='Sample Text - abcde'
           Margin='5,0'
           FontSize=
'{Binding ElementName=sizeSlider, Path= Value}'/>

A binding source is usually a property on an object so you need to provide both the data source object and the data source property in your binding XAML. In the above example the ElementName attribute signifies that you want data from another element on the page and the Path signifies the appropriate property. A binding path can be a complex value, drilling into the member hierarchy, but in this case it's just the name of the property.

&

By: Walt Ritscher

Walt Ritscher has trained thousands of corporate developers during the last twelve years. An active speaker, his teaching schedule has taken him throughout the world providing developer training at corporations, universities, and software conferences. He has collaborated on several books and videos published for the developer market including early adopter .Net courses at Microsoft Press. Walt is currently consulting and teaching .NET and WPF classes for Wintellect. Walt's industry expertise has placed him on various national technology advisory boards. He is also deeply involved in the local developer community-founding the .NET Developers Association in Redmond, WA. Walt has accumulated plenty of experience as a developer and is currently a Microsoft MVP and a member of the Silverlight Insiders. As a Web programmer he has worked numerous projects including: EPA sites and the Microsoft Community Starter Kit.

waltonline@scandiasoft.com

Fast Facts

Data binding is pervasive in WPF and you need to rethink the way you use it in your applications. There is a place for traditional binding where you have a business class value bound to a control property. But there are dozens of dependency properties on a TextBlock. Any of these DPs is a suitable target for a binding. Look at the control and data templates in WPF; they make extensive use of binding.



Article Pages:  1  2 - Next Page: 'Change Notification' >>

Page 1: Flexible and Powerful Data Binding with WPF
Page 2: Change Notification

How would you rate the quality of this article?
1 2 3 4 5
Poor      Outstanding

Tell us why you rated the content this way. (optional)

Average rating:
4.3 out of 5

26 people have rated this article.

Instantly Search Terabytes Of Text
“Lightning Fast”
– Redmond Mag
“Covers all data
sources” – eWeek
25+ fielded & full-text search options
dtSearch’s own document filters highlight hits in popular file types
Web Spider supports static & dynamic data
APIs for .NET, Java, C++, SQL, etc.
Win / Linux (64-bit & 32-bit)
www.dtSearch.com
 

      Sharepoint TechCon

 

SSWUG