The SelectionChanged event is designed to handle controls
that allow multiple selections, so it can be a little confusing for a single-selection selector such as
ComboBox. The SelectionChangedEventArgs type passed to event handlers has two properties of
type IList: AddedItems and RemovedItems. AddedItems contains the new selection and RemovedItems contains the previous selection.
e.g. void ComboBoSelectionChanged(object sender, SelectionChangedEventArgs e) { if (e.AddedItems.Count > 0) object newSelection = e.AddedItems[0]; } Like this code, never assume that there’s a selected item! Besides the fact
that ComboBox’s selection can be cleared programmatically, it can get cleared by the user when
IsEditable is true and IsReadOnly is false. In this case, if the user changes the selection
box text to something that doesn’t match any item, the SelectionChanged event is raised
with an empty AddedItems collection.
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The SelectionChanged event is designed to handle controls that allow multiple selections,
so it can be a little confusing for a single-selection selector such as ComboBox. The
SelectionChangedEventArgs type passed to event handlers has two properties of type
IList: AddedItems and RemovedItems. AddedItems contains the new selection and
RemovedItems contains the previous selection.
e.g.
void ComboBoSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
object newSelection = e.AddedItems[0];
}
Like this code, never assume that there’s a selected item! Besides the fact that ComboBox’s
selection can be cleared programmatically, it can get cleared by the user when IsEditable is
true and IsReadOnly is false. In this case, if the user changes the selection box text to
something that doesn’t match any item, the SelectionChanged event is raised with an
empty AddedItems collection.