gpt4 book ai didi

c# - 更改 listView.sorting 会触发 ItemCheckEventHandler

转载 作者:行者123 更新时间:2023-11-30 22:25:27 28 4
gpt4 key购买 nike

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

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

例如

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。

最佳答案

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

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;
}

使用在

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

listView_ColumnClick(和我修改的地方一样)和

listView_ItemCheck 

if (disableItemCheck_)
return;
//ItemCheck logic

关于c# - 更改 listView.sorting 会触发 ItemCheckEventHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12298539/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com