0
votes

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));
        }
    }
}
1
You didn't say what is the error?clearpath
Edited, what happens is the Visible property stops toggling on MyBool & MyBoolInverse with the last binding statement enabled.CANTPRO
So setting up a delegate anon method for the SelectedValueChanged event of ComboBox works as expected, with the Visible property binding still working. The reverse direction, with a delegate for the PropertyChanged event, will break the form when activated. And I believe that's the case because I'm not casting the enum to the proper type to assign to the SelectedItem or SelectedValue.CANTPRO
Scratch the last sentence above, with the manual binding the form breaks except the ComboBox, it and the bound data update perfectly but the rest of the form is broken.CANTPRO

1 Answers

0
votes

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.