views:

360

answers:

1

Anyone knows how to do this?

I tried this, but it just populates this ComboBox which I already do.

What I need is a way to get the combobox updated whenever the enum property on my object changes:

DataObject.DataEnum

but also get the above Enum updated whenever I change the selected item in the combobox.

Is it possible to do this?

Normally I am used to do the binding this way:

this.TextBox.DataBindings.Add ( "Text", this.DataObject, "Name", false, DataSourceUpdateMode.OnPropertyChanged );

which works great.

+3  A: 

You can use a two-way binding on the SelectedItem property of the ComboBox. When adding values to the combo box, be sure to add the enum values and not just strings that match their display name.

comboBox.Items.Add(ConsoleColor.Red);
comboBox.Items.Add(ConsoleColor.Blue);
// ... etc

Now SelectedItem can be set or get as the enum instead of as a string.

EDIT

It sounds like maybe your object doesn't raise property change notifications which Windows Forms requires to detect that changes to the underlying object need to be refreshed in the UI. Here is an article about how to do that.

EDIT 2

Here's a code sample. I verified this works correctly.

public partial class Form1 : Form {

    private Person p = new Person( );

    public Form1( ) {

        InitializeComponent( );

        comboBox1.DataSource = Enum.GetValues( typeof( Gender ) );

        textBox1.DataBindings.Add( "Text", p, "Name", false, DataSourceUpdateMode.OnPropertyChanged );
        comboBox1.DataBindings.Add( "SelectedItem", p, "Gender", false, DataSourceUpdateMode.OnPropertyChanged );

        label1.DataBindings.Add( "Text", p, "Name", false, DataSourceUpdateMode.Never );
        label2.DataBindings.Add( "Text", p, "Gender", false, DataSourceUpdateMode.Never );

    }
    private void Form1_Load( object sender, EventArgs e ) {
        // yeah, that's right i voted for him,
        // go ahead and downvote me
        p.Name = "John McCain";
        p.Gender = Gender.Male;
    }
    private void Form1_Click( object sender, EventArgs e ) {
        p.Name = "Sarah Palin";
        p.Gender = Gender.Female;
    }
}

public enum Gender {
    Male,
    Female
}

public class Person : INotifyPropertyChanged {

    private string name;
    private Gender gender;

    public string Name
    {
        get { return name; }
        set {
            name = value;
            PropertyChanged( this, new PropertyChangedEventArgs( "Name" ) );
        }
    }

    public Gender Gender {
        get { return gender; }
        set {
            gender = value;
            PropertyChanged( this, new PropertyChangedEventArgs( "Gender" ) );
        }
    }

    public event PropertyChangedEventHandler PropertyChanged = delegate {};

} 
Josh Einstein
So I can use ColourList.DataSource = Enum.GetValues(typeof(Colour)); to populate the ComboBox?
Joan Venge
It's been a while since I've used Windows Forms but yeah I think that should work just fine.
Josh Einstein
Thanks I will try and let you know.
Joan Venge
I am getting the error: "Cannot set the SelectedValue in a ListControl with an empty ValueMember". Do you know why this might be? I fill populate the combobox correctly.
Joan Venge
SelectedValue is used when you bind the ComboBox to a list of objects and want to select one by a property of those objects. Are you trying to use SelectedValue or SelectedItem?
Josh Einstein
Thanks Josh, I replicated this and it worked:http://stackoverflow.com/questions/1502604/how-to-bind-enum-to-winform-combobox-using-objectdatasourceMinus the last part since I already had INotifyPropertyChanged. But this is alot of code for a simple databinding. Do I have to create this list with this custom ListItem type just so that they can be data binded?
Joan Venge
I was able to get this to work with a databinding on SelectedItem property of the ComboBox. I will update my answer with the code snippet. It's a little long because I had to include enum, a class, etc. But it's very simple and straightforward.
Josh Einstein
Thanks Josh, I appreciate it for sure. It's really mind blowing there aren't good source for winforms data binding on the net.
Joan Venge
Yeah binding in Windows Forms 1.0 sucked and by the time 2.0 came out (which was much improved) people had lost confidence. Then it came back with a vengence in WPF.
Josh Einstein
Thanks Josh, well said. It does work great. That's how I expected it to be. I don't know much about wpf. Is databinding really way better?
Joan Venge
Much more powerful. And it's very widely supported in WPF.
Josh Einstein