Listview ItemSelectionChanged fires twice?

后端 未结 4 1089
天涯浪人
天涯浪人 2020-12-16 19:56

I have a Winforms App in C# with a ListView control. This ListView shows a list of TO-DO items and I am using the \'ItemSelectionChanged\' event to handle updates.

<
相关标签:
4条回答
  • 2020-12-16 20:18

    Yes just remove the EventHandler at the start of the refresh and add it again after it has finished refreshing

    i.e

    // Remove handler
    listView1.ItemSelectionChanged -= new ListViewItemSelectionChangedEventHandler(listView1_ItemSelectionChanged);
    
    // Do refresh
    
    // Add again
    listView1.ItemSelectionChanged += new ListViewItemSelectionChangedEventHandler(listView1_ItemSelectionChanged);
    
    0 讨论(0)
  • 2020-12-16 20:19

    I think you need manually unselect the item in the end of your handler.

    listView1.SelectedItem = null;

    0 讨论(0)
  • 2020-12-16 20:20

    Try this:

    private void ItemSelect()
    {
    
            if(SelectedItem!=null)
                App.Current.MainPage.Navigation.PushAsync(new Pages.TLAccByCurrency(), true);
            _selectedItem = null;
    }
    
    0 讨论(0)
  • 2020-12-16 20:21

    Yes, it will fire twice. Once because the previously selected item became unselected, again for the newly selected item. You just have to make sure you see the selection event:

        private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) {
            if (e.IsSelected) {
                // Update form
                //...
            }
        }
    
    0 讨论(0)
提交回复
热议问题