WPF 4.0 Data Binding Change (great feature)

WPF 4.0 has a great change to the data binding pipeline that me and others have been patiently waiting for.  Many thanks to the WPF Team for implementing this feature.

Properties are now automatically reread from the binding source after the UI controls set the property value. 

This feature enables the business entity objects to modify the value coming from the UI and have that value automatically reflected back to the UI.

Example

A classic example of this feature in action would be a state field. 

  • User enters wa in a TextBox
  • State property on the business object is updated to wa
  • Business rules change the case of the entered value to upper case setting the value to WA
  • Data binding pipeline now automatically rereads the State property value and sets the TextBox.Text to WA

Before this great feature, developers could implement a workaround by adding a no-op converter to each TextBox binding.

You can read about the issue that still exists in .NET 3.0 and .NET 3.5 projects here.

This feature is not dependent on the business object implementing the INotifyPropertyChanged interface.

Simple Business Object Code

Public Property State As String
    Get
        Return _strState
    End Get
    Set(ByVal value As String)
        _strState = value
        If Not String.IsNullOrEmpty(_strState) Then
            _strState = _strState.ToUpper
        End If
        OnPropertyChanged("State")
    End Set
End Property

State Field Runtime Behavior Demonstrated

 StateBeforeandAfter

Email Field Runtime Behavior Demonstrated

The Email property business rules will change the TextBox value to lower case.

EmailBeforeandAfter

Download – Requires Visual Studio 2010 Beta1 or later

After downloading the project, please change the extension from .doc to .zip.  This is a requirement of WordPress.com.

Sample Application (80KB)

Have a great day,

Just a grain of sand on the worlds beaches.

8 Responses to WPF 4.0 Data Binding Change (great feature)

  1. forevony says:

    Don’t suppose this might make it into SilverLight?

    sob sob :)

  2. forevony says:

    Well, sadly I dont think it is.

    I’m using 4.0, my boxes dont update like that.

    Many thanks though.

  3. forevony says:

    4.0,
    Just got the email…Yes, I will post somthing.

    (And just double test I’m not being a complete fool…) :)

    Many thanks.

  4. simondraht says:

    After long time looking forward to this great improvement I must see that it doesn’t work in my case:
    I have a C#-project with .NET 4.0 (though some dependencies are still 3.5 SP2, should have no effect, should it?).
    There is a ComboBox with items 1,2,3,4. When selecting an invalid number, the bound property (two-way binding of SelectedItem) should just go back to the previous value. But my ComboBoxes don’t go back, they still pretend to change the bound value.
    Is this limited to certain Controls or datatypes?

Follow

Get every new post delivered to your Inbox.

Join 248 other followers