首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改listView.sorting触发ItemCheckEventHandler

更改listView.sorting触发ItemCheckEventHandler
EN

Stack Overflow用户
提问于 2012-09-06 18:55:54
回答 1查看 445关注 0票数 2

我有一个在第一列有复选框的ListView。在为自定义排序编写一些代码时(当用户单击列标题时),我遇到了一些奇怪的行为。

当用户单击列标题时,我使用ColumnClickEventHandler将listView.Sorting设置为SortOrder.Ascending (或SortOrder.Descending);当命中此行代码时,它似乎会触发ItemCheck事件处理程序。

例如

代码语言:javascript
复制
listView.ItemCheck += new ItemCheckEventHandler(listView_ItemCheck);
listView.ColumnClick += new ColumnClickEventHandler(listView_ColumnClick);
...
...
void listView_ColumnClick(object sender, ColumnClickEventArgs e)
{
   // Determine whether the column is the same as the last column clicked.
   if (e.Column != sortColumn)
   {
      // Set the sort column to the new column.
      sortColumn = e.Column;
      // Set the sort order to ascending by default.
      listView.Sorting = SortOrder.Ascending;
   }
   else
   {
      // Determine what the last sort order was and change it.
      if (listView.Sorting == SortOrder.Ascending)
         listView.Sorting = SortOrder.Descending;
      else
         listView.Sorting = SortOrder.Ascending;
   }
}

在点击行listView.Sorting = SortOrder.Descending;之后,将调用listView.ItemCheck的事件处理程序。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-06 19:51:11

这可能是一个糟糕的技巧,但您可以尝试:

代码语言:javascript
复制
void listView_ColumnClick(object sender, ColumnClickEventArgs e)
{
   listView.ItemCheck -= listView_ItemCheck;
   // Determine whether the column is the same as the last column clicked.
   if (e.Column != sortColumn)
   {

      // Set the sort column to the new column.
      sortColumn = e.Column;
      // Set the sort order to ascending by default.
      listView.Sorting = SortOrder.Ascending;
   }
   else
   {
      // Determine what the last sort order was and change it.
      if (listView.Sorting == SortOrder.Ascending)
         listView.Sorting = SortOrder.Descending;
      else
         listView.Sorting = SortOrder.Ascending;

   } 
   listView.ItemCheck += listView_ItemCheck;
}

使用中使用的布尔属性(private bool disableItemCheck_)

listView_ColumnClick (与我的修改位置相同)和

代码语言:javascript
复制
listView_ItemCheck 

if (disableItemCheck_)
return;
//ItemCheck logic
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12298539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档