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];
}
}
}
- Could the same behavior be achieved with triggers/hooks?
- Is this all I need to do when extending a ComboBox? I mean, will it function like a ComboBox (with the exception of this added behavior) when it comes to styles and such?