-3

I have a ComboBox (using WPF), to which I add Items with C#. Im only adding strings, how can i also add an Event ex. Selected (I want to know which item user selects) to each item I add.

This is how im adding items:

Znamka.Items.Add(temp.Znamka.ToString());

Thanks for your help!

John Doe
  • 23
  • 5
  • 1
    Do these answer your question? [ComboBox- SelectionChanged event has old value, not new value](https://stackoverflow.com/questions/2961118/combobox-selectionchanged-event-has-old-value-not-new-value) **or** [this](https://stackoverflow.com/questions/28374316/event-selectedindexchanged-is-not-found-in-wpf) one **or** [that](https://stackoverflow.com/questions/41009790/c-sharp-wpf-combobox-selectedindex-change?rq=1) one? – Trevor Jan 02 '20 at 14:02
  • @Çöđěxěŕ The first question is the one I needed thanks! – John Doe Jan 02 '20 at 14:10
  • you're welcome, glad to help out. – Trevor Jan 02 '20 at 14:11

1 Answers1

-1

If Znamka is your combo box, you would set an event handler on Znamka for the SelectionChanged event (can be done in XAML). This fires anytime the selection changes, and the actual item selected is referenced in the SelectionChangedEventArgs (AddedItems property) that gets passed to your event handler.

pete the pagan-gerbil
  • 3,136
  • 2
  • 28
  • 49
  • The person above posted a question that answered mine, the solution was the same as you just described, thank you! – John Doe Jan 02 '20 at 14:11
  • What in the `SelectionChangedEventArgs` contains the item selected? `If Znamka is your combo box, you would set an event handler on that object for the SelectionChanged event`, **no**, you can't set that event on that object/string; it's set on the control itself. – Trevor Jan 02 '20 at 14:19
  • The AddedItems property; updated answer to include that. Also made it clearer the subject/target of the text. – pete the pagan-gerbil Jan 02 '20 at 14:23