Reputation: 52381
I have settings dialog with a number of ComboBoxes. More often than not, these ComboBoxes will only have one selectable value. So to make the dialog easier to use I want the ComboBox to autoselect the single value if, and only if, Items.Count == 1 && SelectedItem == null.
I found this but dont want to add additional dependencies if I can avoid it.
I ended up creating a CustomControl based on the ComboBox with a single override:
public class SmartComboBox : ComboBox
{
public SmartComboBox()
{
}
protected override void OnItemsChanged(
NotifyCollectionChangedEventArgs e)
{
base.OnItemsChanged(e);
if (Items.Count == 1 && SelectedItem == null)
{
SelectedItem = Items[0];
}
}
}
Upvotes: 1
Views: 1390
Reputation: 57959
Yes, that's it -- it will work identically, otherwise.
I figured this might be the logical endpoint from your previous question about subscribing to the ComboBox events.
Upvotes: 1
Reputation: 13898
Yes the ComboBox will carry on working perfectly fine, and the other way (personally my prefferend way as i can add mutliple behaviours to a single combobox) is to use Behaviours as suggested in the questions you linked to.
Upvotes: 1