更改listView.sorting火灾ItemCheckEventHandler

问题描述:

我有一个ListView在第一列有复选框。当为自定义排序编写一些代码时(当用户点击列标题时),我遇到了一些奇怪的行为。当用户单击列标题时,我使用ColumnClickEventHandler将listView.Sorting设置为SortOrder.Ascending(或SortOrder.Descending);当这行代码被击中时,它似乎会触发ItemCheck事件处理程序。更改listView.sorting火灾ItemCheckEventHandler

例如

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的事件处理程序被调用。

+0

listView或filesToSyncListView? –

+0

我从来没有做过这样的事情,但通过按标题,不列出选择所有列,并通过触发选定的事件? –

+0

@RaphaëlAlthaus道歉,我简化了这个例子的代码;他们应该有所有阅读listView(现在固定在OP) – HaemEternal

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

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_ColumnClick使用(同一个地方我的修改)布尔属性(private bool disableItemCheck_)和

listView_ItemCheck 

if (disableItemCheck_) 
return; 
//ItemCheck logic 
+0

谢谢!这并没有真正达到为什么它不工作的底部,但它确实完成了修复症状。 – HaemEternal

+0

但是... listView.ItemCheck - = listView_ItemCheck需要去if else之外;因为在else块中进行排序的两项更改也会启动事件处理程序 – HaemEternal

+0

@HaemEternal,您是对的,已更正。 –