paradisonoir
paradisonoir

Reputation: 2910

How to set the text of a ListBox in code-behind design

I have a System.Windows.Control.ListBox, and I would like to select a specific index of it. For example, my listbox is red, white, and blue, and after I read an object/file, I want to select red as the window appears. What is the best way to do that?

public partial class MyWindow :  System.Windows.Window
     {
        public MyWindow()
         {
             InitializeComponent();
             System.Windows.Control.ListBox MyListBox= (ListBox)this.FindName("myListBox");
         } 
     }

Upvotes: 0

Views: 356

Answers (1)

user110714
user110714

Reputation:

You can just use the ListBox.SelectedIndex property...

public MyWindow()
{
    InitializeComponent();
    yourListBox.SelectedIndex = indexOfRed;
}

Upvotes: 1

Related Questions