Search code examples
debuggingbindingsilverlight-5.0

Silverlight 5 binding on a property with logic in its setter does not work as expected when debug is attached


My problem is pretty easy to reproduce. I created a project from scratch with a view model. As you can see in the setter of "Age" property I have a simple logic.

public class MainViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private int age;

        public int Age
        {
            get
            {
                return age;
            }
            set
            {
                /*Age has to be over 18* - a simple condition in the setter*/
                age = value;
                if(age <= 18)
                    age = 18;

                OnPropertyChanged("Age");
            }
        }

        public MainViewModel(int age)
        {
            this.Age = age;
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

In the MainPage.xaml

 <Grid x:Name="LayoutRoot" Background="White">
        <TextBox 
            Text="{Binding Path=Age, Mode=TwoWay}" 
            HorizontalAlignment="Left"
            Width="100"
            Height="25"/>
        <TextBlock
            Text="{Binding Path=Age, Mode=OneWay}"
            HorizontalAlignment="Right"
            Width="100"
            Height="25"/>

    </Grid>

And MainPage.xaml.cs I simply instantiate the view model and set it as a DataContext.

public partial class MainPage : UserControl
{
    private MainViewModel mvm;

    public MainPage()
    {
        InitializeComponent();

        mvm = new MainViewModel(20);
        this.DataContext = mvm;
    }
}

I expect that this code will limit set the Age to 18 if the value entered in the TextBox is lower than 18. Scenario: Insert into TextBox the value "5" and press tab (for the binding the take effect, TextBox needs to lose the focus)

Case 1: Debugger is attached => TextBox value will be "5" and TextBlock value will be "18" as expected. - WRONG

Case 2: Debugger is NOT attached => TextBox value will be "18" and TextBlock value will be "18" - CORRECT

It seems that when debugger is attached the binding does not work as expected on the object that triggered the update of the property value. This happens only if the property to which we are binding has some logic into the setter or getter.

Has something changed in SL5 and logic in setters is not allowed anymore?

Configuration: VisualStudio 2010 SP1 SL 5 Tools 5.1.30214.0 SL5 sdk 5.0.61118.0 IE 10


Solution

  • If someone is interested in the answer, please read here: https://social.msdn.microsoft.com/Forums/en-US/f30a497c-d063-44b7-81ef-f979f186aee8/silverlight-5-binding-on-a-property-with-logic-in-its-setter-does-not-work-as-expected-when-debug-is?forum=silverlightgen