2

最初の列にチェックボックスがある 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 のイベント ハンドラが呼び出されます。

4

1 に答える 1

3

これはおそらく悪いトリックですが、試すことができます:

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(私の変更と同じ場所)および

listView_ItemCheck 

if (disableItemCheck_)
return;
//ItemCheck logic
于 2012-09-06T11:51:10.720 に答える