Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coerce Value in Property Setter - Silverlight 5

In the example below, we have bound a ViewModel to a view with a single property called Message. This property is bound to a TextBox with a two way binding. For this test, we're doing some value coercion in the setter and raising the property changed again.

In Silverlight 4, this worked perfectly. If the message property changed in the property setter, the textbox would see the new value. E.g. typing "A" into the textbox and losing focus would result in Aaaaaaaaaaa appearing as the value was changed.

In Silverlight 5 however, this seems to be broken/changed. The getter is never hit after the value is modified in the setter. Adding a IValueConverter in between, shows that the Convert/ConvertBack methods are never hit. It seems that something fundamental has changed between version 4 and 5. Has there been any changes? Is this a bug?

public class ViewModel : INotifyPropertyChanged
{
    private string _message;

    public event PropertyChangedEventHandler PropertyChanged;

    public string Message
    {
        get
        {
            return _message; 
        }
        set
        {
            _message = value;
            this.RaisePropertyChanged();

            if (_message.Length < 10)
            {
                _message = _message.PadRight(10, 'a');
                this.RaisePropertyChanged();
            }
        }
    }

    private void RaisePropertyChanged()
    {
        var handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs("Message"));
        }
    }
}
like image 343
Ray Booysen Avatar asked Aug 15 '12 07:08

Ray Booysen


1 Answers

There are a couple of workarounds for this issue which appears to be a bug in Silverlight 5's binding debugging feature (see @Ray Booysen's answer).

First though, it is important to know that this is not an issue in production but rather, only occurrs while debugging the application. The workarounds are therefore really only to reproduce the production behaviour in the debug environment (albeit with some of the debugging features disabled).

The first of the work arounds is to disable binding debugging using the static (shared in VB.Net) field named IsDebuggingEnabled in the Binding class. The documentation there provides the following recommendation.

set this field to false in your application class constructor

Note: This change cannot be restricted to a single binding but rather will affect all bindings in the application.

The second is to disable the Silverlight debugger in the project properties for the Web project that is hosting the Silverlight application. Make this change using the following steps.

  1. Right click on the Web project in Solution Explorer and select Properties.
  2. Select the Web tab.
  3. Scroll down to the Debuggers section.
  4. Uncheck the checkbox labelled Silverlight.

Note: This change disables not only binding debugging for the application but also general Silverlight debugging. Other debuggers can however be enabled.

like image 189
Scott Munro Avatar answered Oct 06 '22 20:10

Scott Munro