ListView控件SelectedItemChanged事件 [英] ListView Control SelectedItemChanged event

查看:77
本文介绍了ListView控件SelectedItemChanged事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,

我有兴趣获得一个始终选择项的listview控件.换句话说,如果在列表视图中选择了项目编号1,并且用户再次单击控件而不是任何项目(这通常会取消选择任何选中的项目),则我希望控件保留原始选择的项目.

我一直在努力使它正常工作,重写了onselecteditem更改的方法和onmouse down的方法,但是似乎没有任何东西可以满足我的需求.

其他人都有一些可能会起作用的代码我想要什么?

谢谢,

尼克

All,

I'm interested getting a listview control that always has an item selected.  In otherwords, if item number 1 in a listview is selected and the user click again on the control not on any item (this normally de-selects any selected item), I want the control to keep the originally selected item.

I've been trying all day to get this to work, overriding the onselecteditem changed method and onmouse down method, but nothing seems to give me what I want.

Anyone else have some working code that might do what I want?

thanks,

Nick

推荐答案

这是我所能做到的.控件的行为不如您所说的那样,但这是正确的事件,现在仅需要适当的逻辑.

This is as far as I could go. The control doesn't behave as you said it should, but it's the right event and only the proper logic is now necessary.

<身体>
公众类UnSelectableListView:ListView
{
私有列表 < ListViewItem _ lastSelectedItems;
公用public UnSelectableListView()
{
_ lastSelectedItems = 列表 < ListViewItem > ();
受保护的覆盖无效void OnSelectedIndexChanged(EventArgs e)
.{
base.OnSelectedIndexChanged(e);
if(SelectedItems.Count > 0)
foreach(SelectedItems中的ListViewItem项目)
_lastSelectedItems.Add(item);
否则
foreach(_lastSelectedItems中的ListViewItem项目)
item.Selected = > true ;
中断;
    public class UnSelectableListView : ListView  
    {  
        private List<ListViewItem> _lastSelectedItems;  
 
        public UnSelectableListView()  
        {  
            _lastSelectedItems = new List<ListViewItem>();  
        }  
 
        protected override void OnSelectedIndexChanged(EventArgs e)  
        {  
            base.OnSelectedIndexChanged(e);  
 
            if (SelectedItems.Count > 0)  
                foreach (ListViewItem item in SelectedItems)  
                {  
                    _lastSelectedItems.Add(item);  
                }  
            else  
                foreach (ListViewItem item in _lastSelectedItems)  
                {  
                    item.Selected = true;  
                    break;  
                }  
        }  
    } 


这篇关于ListView控件SelectedItemChanged事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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