2009-11-25 89 views
0

我有两个ListViews在我的应用程序,与最初相同的列集合。当用户重新排列列中的一列时,我希望另一列中的列被重新排序。如何在两个列表视图中同步列顺序?

我有意见ColumnReordered事件之一下面的事件处理程序,以及其他相应的处理程序:

private volatile bool reorderingColumns; 

private void listView1_ColumnReordered(object sender, ColumnReorderedEventArgs e) 
{ 
    // Prevent reentry - is this necessary? 
    if (reorderingColumns) 
     return; 

    try 
    { 
     reorderingColumns = true; 

     // copy display indices to the other listView 
     for (int i = 0; i < columnInfo.Count; i++) 
     { 
      listView2.Columns[i].DisplayIndex = listView1.Columns[i].DisplayIndex; 
     } 
    } 
    finally 
    { 
     reorderingColumns = false; 
    } 
} 

然而,列在第二列表视图的顺序保持不变。我需要做什么才能让第二个列表视图以新顺序重新绘制它的列?

回答

1

这是因为重排序事件在列显示索引实际更改之前触发。这将工作:

private void listView1_ColumnReordered(object sender, ColumnReorderedEventArgs e) 
{ 
    listView2.Columns[e.Header.Index].DisplayIndex = e.NewDisplayIndex; 
} 

编辑:我想补充,你不会需要reorderingColumns检查任何更多的采用这种方法。

+0

是否有事件在其更改后触发? – Simon 2009-11-25 15:37:14

+0

我不这么认为。你可能可以实现你自己的。 – GenericTypeTea 2009-11-25 16:06:54