tags:

views:

39

answers:

1

In the WPF ListBox control, I am trying to update the ListBox DataContext and select the last one item. But it fails.

public void Update_Button_Click()
{
    this.MyListBox.BeginInit();
    this.MyListBox.DataContext = family; // family is a collection of Person object.
    this.MyListBox.EndInit();
    this.MyListBox.SelectedIndex = family.Count - 1;
}

But no item is selected in ListBox

<ListBox Name="MyListBox" ItemsSource="{Binding}"/>

I tried to update the SelectedIndex in handler of SourceUpdated event, it also fails. Please give me any clue or help. Thanks~

+1  A: 

I tried your example and it works fine. Is there anything else in your code that may affect this?

karmicpuppet
Yes, after I remove all the other code, it works again. It due to the Animation of selected item's width. I will try again and find another way to do the animation.
Chuanyu Wang