Eto ComboBox with dynamically extendable Binding

Hello @dale, @curtisw or other Eto specialists,

while trying to understand how to bind different Eto controls to ViewModel properties, I came across this great example of a DropDown bound to a ObservableCollection . The ObservableCollection is filled with ListItem Elements also found in an Enum defined beforehand. The SelectedItem of the Dropdown stays selected even if it’s ListItem value is changed.

Now I’m trying to replace the predefined Enum with something that can be extended dynamically during runtime to (re)fill the Dropdown with a different number of items.

I tried to exchange the Enum directly with a string but as soon as I do that, the Dropdown seems to loose its Selected Item and gets emptied out. Probably this is to be expected as the changed string can’t be found anymore…

Do you have a tip on how to bind a Dropdown to something more dynamic than an Enum?

For reference I’m attaching my dysfunctional attempt to exchange the Enum with string:
MainForm.cs (2.8 KB)

Thank you!

Hey @romio82,

Since you are replacing the item (and changing the key) in the collection, the selected item changes. If you want to change the selected index, you can set it by using your commented SelectedKeyBinding for the combo to ensure the MyModel.Resolution refers to the selected item, then setting the Resolution property during the command. E.g.

return new Command((sender, e) => {
    
    var firstResolutionSelected = Resolution == ResolutionNames[0].Key;
    ResolutionNames[0] = new ListItem
    {
        Key = "Viewport " + count,
        Text = "Viewport " + count
    };
    // update to the new selection.
    if (firstResolutionSelected)
        Resolution = "Viewport " + count;
    count++;
});

Hope this helps!

Great, thanks a lot for that immediate and helpful answer, @curtisw!
That works perfectly and seems to be quite obvious now that you pointed me towards it :slight_smile:

Somewere along the way I seem to have lost the fact that I have all the ViewModel Properties at hand in the Command and can work with them. Quite convenient and a nice learning!
Thank you very much!

1 Like