Tag Archives: wpf

“The .Net Continuum” Laurant Bugnion

[MS Techdays 2009] Summary “The .Net Continuum” Laurant Bugnion

Laurant Bugnion explained that Microsoft worked hard to expose WCF to all the presentation environments. The presentation continuum going from basic ASP.NET to Silverlight and WPF can now connect to WCF.

WPF1

The next part of the presentation explained the difficulties of connecting the presentation platforms to WCF if they are hosted on different serves resulting in cross-domain calls between the web server and WCF. The questions at hand was, is it possible to go direct to WCF or are we forced to go through the web server.

WPF2

Summary:

  • HTML: here there is no choice; you?l always have to pass through the web server. The web server will be the client of WCF.
  • AJAX: cross domain calls will result in the user being prompted in other cases AJAX will connect to WCF directly.
  • Silverlight: if the correct policies, client access policies, are in place WCF can be reached directly.
  • WPF: no problem, direct access is always possible.

The last part of the presentation was about the ViewModel pattern. This pattern avoids having your Silverlgiht or WPC logic connected directly, although some proxy is in between, to your data in WCF. It is the MVC for declarative and data-bound interfaces. The idea is to go from …

WPF3

to …

WPF4

… .

The ViewModel pattern removes the mixing of presentation and logic when you?e using codebehind during the development of your UI. The data is presented as properties of the ViewModel and consumed through normal databinding. Operations on the data, events, are exposed as methods of the ViewModel.

For code-examples and a detailed discussion of the ViewModel pattern have a look at: http://www.nikhilk.net/Silverlight-ViewModel-Pattern.aspx

“Databinding in WPF” Gill Cleeren

[MS Techdays 2009] Summary “Databinding in WPF” Gill Cleeren

This was one of the presentations I didn’tt find very interesting so I haven’t a lot of notes. For details you should have a look at the conference slides.

Why Databinding
Databinding is not unique to WPF it appeared already in ASP.Net and Winforms applications. Not using databinding means writing a lot of code: code to get controls in an initial state, code to handle state update events (data object’s state) and code to update the visual controls (representation of the object on the GUI). This plumbing code is taken care of by databinding. A common mistake is thinking that databinding requires a DB. Other sources are supported as well.

What is Databinding
The basic concepts can be summarized as follows: properties of a control are bound to properties of a data object. Databinding, in other words, glues properties together.

databinding1

Databinding requires 4 things:

  • a source object
  • a target object
  • a source Property
  • a target Property

The SetBinding method links them together:

databinding2

databinding3

Binding can also be done by means XAML through Dependency Properties:

databinding4

The ElementName is the source of the binding. For XAML you?e required to add code responsible to handle change events by implementing INotifyPropertyChanged or INotifyCollenctionChanged. DataTemplates allow you to apply different visual templates to a control based on the data’s content.

Types of Binding

  • OneWay: Target updates when source changes
  • TwoWay: Change to either changes the other
  • OneTime: Similar to OneWay, changes aren’t reflected in Target (snapshot view)
  • OneWayToSource: Source updates when Target changes

The update events fire when the control loses focus, when a property in the source or target has changed or whenever an explicit call to the UpdateSource method is made.

Value Conversions, Grouping and Sorting

Value Conversion, before-after:

databinding5

Value conversions allow you to morph source values to different target values. This is done through the use of the ValueConversion attribute and the Convert and ConvertBack methods.

Bound collections can be grouped and sorted, see examples below, through views on the collection by implementing the ICollectionView:

databinding6

databinding7

Validation
Two types of validation are supported:

  • Exception validation rules: used when your data objects already have validation in them
  • Custom validation rules: if validation is added for the first time.

You can modify the presentation of a control when the validation has failed through setting the Validation.ErrorTemplate.

Data Providers
Two generic data providers are available:

  • XmlDataProvider: to databind XML, the data is gathered through XPath expressions
  • ObjectDataProvider: objects can be anything for example XAML declarations

Summary
The goal of databinding is to reduce the tedious coding needed to glue the interface controls to data objects holding the state. Syntax can be daunting to begin with but allows for a high degree of customization.