Binding causes StackOverflow

前端 未结 2 425
死守一世寂寞
死守一世寂寞 2020-12-12 03:40

Im not sure what I am doing wrong here.

Lets say, I have two UserControls BoxAand BoxB. Both have a DependencyProperty called Text

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-12 04:12

    With any form of Change Notificaiton, one danger is what I call the "Ping Pong" problem. Example:

    1. Property A changes.
    2. Property B is changed to match A.
    3. Property B changed.
    4. Property A is changed to match B.
    5. Recurse to 1

    In order to avoid that, the exampel code for Properties with Change notificaiton looks like this:

    public string PhoneNumber
    {
        get
        {
            return this.phoneNumberValue;
        }
    
        set
        {
            if (value != this.phoneNumberValue)
            {
                this.phoneNumberValue = value;
                NotifyPropertyChanged();
            }
        }
    }
    

    If the input is the same as output, nothing is done. The squence goes:

    1. Property A changes
    2. Property B is chagned to match A
    3. Proeprty B changes
    4. Property A notices it already has that value anyway, so nothing is done.

    My best guess is that WPF Elements have no such protection. It is one of those cases were "trying to be smart could result in being really dumb".

提交回复
热议问题