Reputation: 485
I have a dynamically created ComboBox that I need to set the SelectionChanged property for. How can I do this from code?
ComboBox comboBox = new ComboBox()
{
Background = Brushes.GhostWhite,
BorderBrush = Brushes.Gainsboro,
BorderThickness = new Thickness(1),
Margin = new Thickness(10),
ItemsSource = new ObservableCollection<string>(list),
SelectionChanged = "comboBox_SelectionChanged" //SelectionChanged is not a valid property
};
Upvotes: 0
Views: 351
Reputation: 128067
You would have to attach a SelectionChanged event handler like this:
var comboBox = new ComboBox { ... };
comboBox.SelectionChanged += comboBox_SelectionChanged;
The above assumes that there is a handler method like
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
...
}
Upvotes: 2
Reputation: 89295
SelectionChanged
is not a property, it is an event. You are trying to attach event handler to an event using object initializer syntax, and .NET doesn't seems to support that.
Here are some related questions :
Upvotes: 0