【问题标题】:How can I synchronise column orders in two listviews?如何在两个列表视图中同步列顺序?
【发布时间】:2009-11-25 14:57:26
【问题描述】:

我的应用中有两个 ListView,它们的列集合最初是相同的。当用户对一个列重新排序时,我希望另一个列重新排序。

我在其中一个视图的 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;
    }
}

但是,第二个列表视图中的列顺序保持不变。我需要做什么才能让第二个列表视图以新顺序重绘其列?

【问题讨论】:

    标签: c# .net winforms listview


    【解决方案1】:

    这是因为 reordered 事件在列的显示索引实际更改之前触发。这将起作用:

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

    编辑:只需添加,使用此方法您将不再需要reorderingColumns 检查。

    【讨论】:

    • 是否有事件发生变化后触发?
    • 我不这么认为。您可能可以实现自己的。
    猜你喜欢
    • 2015-08-27
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 2013-07-18
    • 2015-03-20
    • 2019-06-22
    • 1970-01-01
    • 2015-08-22
    相关资源
    最近更新 更多