开发者

WPF: listview SelectedIndex returns -1 if binded data is updated?

I'm displaying data in a ListView. The data (gotten through a DLL and set in an ObservableCollection) is updated every 3 seconds using a TimerCallback. I'm using data-binding between my ListView and the data.

I'm adding right-click menu for the list view items. It seems like when the data gets updated, my listview's SelectedIndex will return -1. If I use the right-click menu very fast or set the TimerCallback to be less fr开发者_JAVA百科equent, I don't get SelectedIndex equal -1 as often.

1-Is my assumption correct?

2-If so, how can I get around this? Can I detect when the right-click menu pops up and just disable background data update on TimerCallback? Any better approach?

Thanks!


When you are binding your ListView ItemsSource to your ObservableCollection, the SelectedItem informations will be preserved only if you keep the same objects.

If you are using a code like this to update your ItemsSource

yourListView.ItemsSource = newObsevableCollection;

you will lost the update notification and the selected item info because you use new object instance.

The solution is to keep the same objects and just update the values and implements NotifyChanged event to update the ListView

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜