C# 更改listView.sorting将触发ItemCheckEventHandler

C# 更改listView.sorting将触发ItemCheckEventHandler,c#,.net,C#,.net,我有一个列表视图,第一列有复选框。在为自定义排序编写代码时(当用户单击列标题时),我遇到了一些奇怪的行为 当用户单击列标题时,我使用ColumnClickEventHandler将listView.Sorting设置为SortOrder.Ascending(或SortOrder.Descending);当这行代码被命中时,它似乎会触发ItemCheck事件处理程序 乙二醇 在listView.Sorting=SortOrder.Descending行之后;单击listView的事件处理程序。调用

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

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

乙二醇


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

这可能是一个坏把戏,但您可以尝试:

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\u column单击
(与我的修改位置相同)并


listView或FileToSyncListView?我从未做过类似的事情,但按header键,不会列出选择的所有列并触发选择的事件?@RaphaëlAlthaus抱歉,我是在简化示例代码;它们应该都读取listView(现在在OP中修复了)@AleksandarToplek,除非我实际更改排序属性,否则它不会命中事件处理程序;所以我不认为这是一个焦点问题。@RaphaëlAlthaus现在我想了一点,检查状态可能没有从上一个状态更改,所以在listViewëu ItemCheck中,您可以检查“检查”值是否更改。事件可能被调用,但实际上“检查”值保持不变。谢谢!这并没有真正弄清为什么它不起作用,但它在修复症状方面做得很好。然而。。。listView.ItemCheck-=listView\u ItemCheck需要跳出if-else;由于else块中对排序的两个更改也将启动事件处理程序
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;
}
listView_ItemCheck 

if (disableItemCheck_)
return;
//ItemCheck logic