开发者

How to select one item in ListBox

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 th开发者_JAVA百科e SelectedIndex in handler of SourceUpdated event, it also fails.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜