简体   繁体   中英

DataBinding between multiple controls and a single object

Just getting started with data binding in C# and looking for some help. The below binding statements break (Visible property stops toggling with MyBool & MyBoolInverse) when the line binding SelectedItem of the combo box to MyEnumVar of the BusinessObject executes. Binding directly to the object instead of the BindingSource, or binding to SelectedValue instead of SelectedItem, has the same effect. Further, the value of MyEnumVar doesn't change with selections to the combo box. What am I doing wrong?

public partial class Form1 : Form
{
    BindingSource bs = new BindingSource();
    private BusinessObject bo = new BusinessObject();

    public Form1()
    {
        InitializeComponent();
        bs.DataSource = bo;

        // Checkbox determines what type of dialog to display.
        boolCheckBox.DataBindings.Add("Checked", bs, "MyBool", true,
            DataSourceUpdateMode.OnPropertyChanged);

        trueBox.DataBindings.Add("Visible", bs, "MyBoolInverse");
        falseComboBox.DataBindings.Add("Visible", bs, "MyBool");
        falseBox.DataBindings.Add("Visible", bs, "MyBool");
        falseButton.DataBindings.Add("Visible", bs, "MyBool");

        myEnumComboBox.DataSource = Enum.GetValues(
            typeof(BusinessObject.MyEnum));
        // Line below breaks above bindings, same for SelectedValue.
        myEnumComboBox.DataBindings.Add("SelectedItem", bs, "MyEnumVar");
    }
}
class BusinessObject : INotifyPropertyChanged
{
    public enum MyEnum { RED, BLU }
    MyEnum _MyEnumVar;
    public MyEnum MyEnumVar
    {
        get { return _MyEnumVar; }
        set
        {
            if (value != _MyEnumVar)
            {
                _MyEnumVar = value;
                NotifyPropertyChanged("MyEnumVar");
            }
        }
    }

    private bool _MyBool;
    public bool MyBool
    {
        get { return _MyBool; }
        set
        {
            if (value != _MyBool)
            {
                _MyBool = value;
                MyBoolInverse = !value;
                NotifyPropertyChanged("MyBool");
            }
        }
    }

    private bool _MyBoolInverse;
    public bool MyBoolInverse
    {
        get { return _MyBoolInverse; }
        private set
        {
            if (value != _MyBoolInverse)
            {
                _MyBoolInverse = value;
                NotifyPropertyChanged("MyBoolInverse");
            }
        }
    }

    public BusinessObject()
    {
        MyBoolInverse = !MyBool;
        MyEnumVar = MyEnum.BLU;
    }

    // Boilerplate INotifyPropertyChanged implementation & helper.
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName = "")
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Visible property has problems with binding. Try manual 'binding'. Something along the lines of

trueBox.Visible = bo.MyBoolInverse;
bo.PropertyChanged += (s, e) => { 
  if(e.PropertyName == "MyBoolInverse") 
    trueBox.Visible = bo.MyBoolInverse; 
};

Edit: Also, binding to MyEnumVar is not working beacause it is not declared as a public property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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