简体   繁体   English

从另一个绑定属性的设置器设置一个绑定属性的值

[英]Setting value of a bound property from the setter of another bound property

I have a business object in C# which implements INotifyPropertyChanged and contains several bound properties. 我在C#中有一个实现INotifyPropertyChanged并包含几个绑定属性的业务对象。 In a nutshell, it looks like this: 简而言之,它看起来像这样:

public class BusinessObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, e);
        }
    }

    private int _intProperty;
    public int IntProperty // bound to NumericUpDown control
    {
        get { return _intProperty; }
        set
        {
            if (_intProperty == value)
            {
                return;
            }

            _intProperty = value;
            OnPropertyChanged(new PropertyChangedEventArgs("IntProperty"));

            // if IntProperty is > 10, then set BoolProperty to false
            if (value > 10)
            {
                this.BoolProperty = false;
                //OnPropertyChanged(new PropertyChangedEventArgs("BoolProperty"));
            }
        }
    }

    private bool _boolProperty;
    public bool BoolProperty // bound to CheckBox
    {
        get { return _boolProperty; }
        set
        {
            if (_boolProperty == value)
            {
                return;
            }

            _boolProperty = value;
            OnPropertyChanged(new PropertyChangedEventArgs("BoolProperty"));
        }
    }

As you can see in the setter for IntProperty, I'm setting the BoolProperty = false when IntProperty has been set > 10. BoolProperty is bound to a CheckBox in my UI (winforms) but even though I'm setting BoolProperty = false, the CheckBox doesn't update to reflect that change until the control that's bound to IntProperty loses focus. 正如您在IntProperty的设置器中看到的那样,当IntProperty设置为> 10时,我将BoolProperty = false设置。BoolProperty绑定到我的UI(winforms)中的CheckBox,但是即使我将BoolProperty = false设置为,在绑定到IntProperty的控件失去焦点之前,CheckBox不会更新以反映该更改。 I thought maybe I needed to call OnPropertyChanged after I set BoolProperty = false but that didn't seem to make a difference. 我以为可能在设置BoolProperty = false之后需要调用OnPropertyChanged,但这似乎没有什么不同。 Is this the expected behavior in this scenario? 这是这种情况下的预期行为吗? If so, is it possible to implement the behavior that I've described? 如果是这样,是否有可能实现我描述的行为?

您可能需要将绑定的DataSourceUpdateMode设置为DataSourceUpdateMode.OnPropertyChanged

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM