listebox selecteditem at runtime in WPF

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;

Published by


A developer,teacher and a blogger obsessed with Python,Dart and open source world from India

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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