locked
ListBox highlighting after SelectedIndex is set RRS feed

  • Question

  • I have the following line of code after I set a ListBox DataContext to a DataTable (and after checking if there is an item to select):
    this.myListBox.SelectedIndex = 0;

    The SelectedIndex returns as 0 but the item is not highlighted.  I have to click on the item with the mouse to highlight it.  How can I highlight it through code?
    Thursday, October 9, 2008 7:03 PM

Answers

  • Ahhh, duh.  I didn't think about Focus.  
    • Marked as answer by Kevin Jonas Friday, October 10, 2008 3:02 PM
    Friday, October 10, 2008 3:02 PM

All replies

  • Hi,

    I think it gets highlighted .Just explicitly bring focus to the listbox and you will see the highlight.

    ListBoxItem ob = null
                for (int i = 0; i < 10; i++) 
                { 
                    ob = new ListBoxItem(); 
                    ob.Content = "List" + i; 
                    listBox1.Items.Add(ob); 
                } 
     
                listBox1.SelectedIndex = 5
                listBox1.Focus(); 


    SelectedItem can also be used.But here you'll have to give the object.
     listBox1listBox1.SelectedItem = listBox1.Items.GetItemAt(6); 


    Hope it helps you




    FEAR NOT TO BE JUST
    • Proposed as answer by DutchMarcel Friday, October 10, 2008 7:58 AM
    Friday, October 10, 2008 5:43 AM
  • Ahhh, duh.  I didn't think about Focus.  
    • Marked as answer by Kevin Jonas Friday, October 10, 2008 3:02 PM
    Friday, October 10, 2008 3:02 PM
  • Hi,

    Why not mark it as answer then....:)

    Thank You

    FEAR NOT TO BE JUST
    Sunday, October 12, 2008 1:28 PM
  • Hi,

    Hey Nobody to mark this :)

    Thank You






    FEAR NOT TO BE JUST
    Tuesday, October 21, 2008 6:33 AM