在追赶的ListView取消选择所有 - 射击的SelectedIndexChanged两次 [英] Catching Unselecting All in ListView -- SelectedIndexChanged Firing Twice

查看:208
本文介绍了在追赶的ListView取消选择所有 - 射击的SelectedIndexChanged两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有一个项目的ListView。当选择了一个,一个文本框,映入眼帘的,以显示该项目的更多细节的权利(花费项目数据一点点时间来抓住)。我要去的行为是,当所有项目获得未选中隐藏在右边这个文本框。

Basically, I have a ListView of items. When one is selected, a text box comes into view on the right to display more details of that item (takes a little time for the item data to be grabbed). The behavior I was going for was to hide this text box on right when all items get unselected.

        private void listView1_SelectedIndexChanged(object sender, EventArgs e) {

            // should only be 1 item selected, it's not a multi-select listview
            ListView.SelectedListViewItemCollection collection = this.listView1.SelectedItems;

            if (collection.Count == 0) {
                this.label2.Visible = false;
            }    
            foreach (ListViewItem item in collection) {
                this.label2.Visible = true;
                getSideInformation(item.Text);
            }
        }

我注意到盒子的闪烁,当我简单地选择其他项目。我没有改变我的code一些挖:

I noticed a flicker of the box, when I am simply selecting another item. I did some digging by changing my code to:

        private void listView1_SelectedIndexChanged(object sender, EventArgs e) {

            // should only be 1 item selected, it's not a multi-select listview
            ListView.SelectedListViewItemCollection collection = this.listView1.SelectedItems;

            if (collection.Count == 0) {
                this.label2.Text = "Unselected all!"    
            }
            foreach (ListViewItem item in collection) {
                getSideInformation(item.Text);
            }
        }

基本上,我不再隐藏框,但只是改变了文本,如果它与选择0项的选择更改事件。我发现了什么是该事件实际上两次触发一个简单的(0项,并且第二次用新的项目中选择一次)选择另一个项目。所以,我的盒子将始终显示未选择的一切!而它的加载了任何副作用的信息,如果我有previously选择的项目,并切换到另一个项目。

Basically, I no longer hide the box, but just change the text if it's a selection change event with 0 items selected. What I found out is that this event actually fires TWICE for a simple select another item (once with 0 items, and a second time with the new items selected). So my box will always display "Unselected all!" while it's loading up any side information if I had previously selected an item and was changing to another item.

有没有什么方法来区分未选中,与0的项目,最初烧制的选择另一个项目来呢?

Is there any way to differentiate an actual event firing of all items unselected versus that initial firing of 0 items for the selecting another item case?

推荐答案

您可以到ListView的ItemSelectionChangedEvent,而不是注册。

You can register to ListView's ItemSelectionChangedEvent instead.

        this.listView1.ItemSelectionChanged += this.HandleOnListViewItemSelectionChanged;

        private void HandleOnListViewItemSelectionChanged(Object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (e.IsSelected)
            {
                this.detailsLabel.Text = this.GetDetails(e.Item);
            }
            else
            {
                this.detailsLabel.Text = String.Empty;
            }
        }

这篇关于在追赶的ListView取消选择所有 - 射击的SelectedIndexChanged两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆