c#ObjectListView - 更改vlue后不更新排序

时间:2018-04-30 08:28:24

标签: c# objectlistview

我正在使用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岁的对象,则排序不会更新并且它仍处于相同的位置,当排序列的任何值发生更改时,我需要做什么才能使其自动排序更新?

2 个答案:

答案 0 :(得分:0)

使用此:

listView.SetObjects(iEnumerable)

答案 1 :(得分:0)

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

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

也许这有助于其他任何人。