简体   繁体   English

获取INotifyPropertyChanged类以返回List.Count

[英]Getting INotifyPropertyChanged class to return List.Count

I have the following class: 我有以下课程:

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;
 }

I then bind this class to dataGrid.DataSource and when ever Tasks variable change, it gets updated live in the datagrid. 然后,我将该类绑定到dataGrid.DataSource,并且每当Tasks变量更改时,它都会在datagrid中实时更新。 The problem is instead of int I need to use List where all the tasks are stored and I want Tasks to return List.Count instead of int tasks like this: 问题是不是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");
            }
        }
    }

But dataGrid doesn't update automatically, I have to refresh it to see the update. 但是dataGrid不会自动更新,我必须刷新它才能看到更新。 Any suggestions how to handle this? 有什么建议如何处理吗? Thanks! 谢谢!

List does not implement INotifyPropertyChanged or any other mechanism that lets you know when its contents are changed. List不实现INotifyPropertyChanged或任何其他机制来通知您何时更改其内容。 You should use ObservableCollection<T> instead. 您应该改为使用ObservableCollection<T>

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

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