listebox selecteditem at runtime in WPF

Let’s learn how to set selected item of Listbox ,in WPF at runtime,


Listbox’s selected item property used to get/set values at runtime. In WPF you can used ViewModel to get selected item automatically to the ViewModel, that is for another article. Let’s learn how to set selected item at runtime,

  • First we need to identify the element in List Item collection
  • Set the property value

Getting the Item

var itt = (ListBoxItem)lstb_base.Items.Cast<ListBoxItem>().Where((s, b) => s.Content.Equals(r.M_Base)).SingleOrDefault();
                             

Set property of SelectedItem

lstb_base.SelectedItem = itt;

Author: Manoj

Developer and a self-learner, love to work with Reactjs, Angular, Node, Python and C#.Net

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.