首页 > 解决方案 > c# ObjectListView - 更改 vlue 后不更新排序

问题描述

我正在使用 ObjectListView 来显示字典值,它的工作很完美,但是当我对它进行排序一次时,我的意思是当我按年龄降序排序时,它就像{30, 27, 26, 15, 10}

这是我的对象类的一个例子

public class Item: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    int age = -1;
    public Int32 Age
    {
        get{return age;} 
        set
        {
            if (value == age)
                return;
            this.age = value;
            OnPropertyChanged("Age");
        }
    }
}

这也是我如何将对象列表附加到列表视图

System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
try
{
    this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
    stopWatch.Start();
    this.listview1.SetObjects(this.ItemsDictionary.Values);
}
finally
{
    stopWatch.Stop();
    this.Cursor = System.Windows.Forms.Cursors.Default;
}

我用了

this.listview1.Update();

但我知道这是更新控件的绘画而不是排序。

如果 15 岁的对象被更新(增加或减少)排序不会更新并且它仍然在相同的位置,当排序列的任何值发生更改时,我需要做些什么来使其自动排序更新?

标签: c#objectlistview

解决方案


我通过对我更新的每个项目使用此代码来解决它

    this.listview1.Sort(this.listview1.LastSortColumn);
    this.listview1.UpdateObject(OBJ);

也许这对其他人有帮助。


推荐阅读