繁体   English   中英

获取INotifyPropertyChanged类以返回List.Count

[英]Getting INotifyPropertyChanged class to return List.Count

我有以下课程:

public partial class Operation : INotifyPropertyChanged
{
    private int tasks;

    public int Tasks
    {
        get
        {
            return this.tasks;
        }
        set
        {
            if (this.tasks != value)
            {
                this.tasks = value;
                this.OnPropretyChanged("Tasks");
            }
        }
    }

    protected virtual void OnPropretyChanged(string propertyName)
    {
        var handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
 }

然后,我将该类绑定到dataGrid.DataSource,并且每当Tasks变量更改时,它都会在datagrid中实时更新。 问题是不是int,我需要使用List来存储所有任务,并且我希望Tasks返回List.Count而不是像这样的int任务:

    public int Tasks
    {
        get
        {

            return this.TASKS.Count;
        }
        set
        {
            if (this.tasks != TASKS.Count)
            {
                this.tasks = TASKS.Count;
                this.OnPropretyChanged("Tasks");
            }
        }
    }

但是dataGrid不会自动更新,我必须刷新它才能看到更新。 有什么建议如何处理吗? 谢谢!

List不实现INotifyPropertyChanged或任何其他机制来通知您何时更改其内容。 您应该改为使用ObservableCollection<T>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM