如何使用get accessor更新UI? [英] How to update UI with only get accessor?

查看:56
本文介绍了如何使用get accessor更新UI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好, 

Hi All, 

我有一个持有成员集合的班级。我必须在UI中显示某些成员的数量。

I have a class which holds the collection of a members. And I have to show the count of certain members in the UI.

我的计划是拥有一个只有Get访问者的属性并将其绑定到UI,但是UI不会随时更新从集合中添加/删除新成员。 

My plan is to have a property with only Get accessor and bind it to the UI, but the UI is not updated whenever a new member is added/deleted from the collection. 

我的实现如下,这是有效的。但是可以在同一属性的Get访问器中设置值。还有其他更好的实施吗?我不想在"会员"上调用  CollectionChanged事件。
集合并调用"NotifyPropertyChanged(" MembersCount")";"每次添加或删除成员时。

My implementation is as follows, and this is working. But is it okay to set the value in the Get accessor of the same property. Is there any other better implementation? I do not want to invoke the CollectionChanged event on the "Members" collection and call the "NotifyPropertyChanged("MembersCount");" every time a member is added or deleted.

    public class Employee
    {
        public int Experience { get; set; }
    }
    public class Manager:NotificationBase
    {
        private int _membersCount;
        public ObservableCollection<Employee> Members { get; set; }

        public int MembersCount
        {
            get
            {
                if (Members != null)
                    MembersCount = Members.Count(p => p.Experience > 5);
                return _membersCount;
            }
            private set
            {
                _membersCount = value;
                NotifyPropertyChanged("MembersCount");
            }
        }
    }







Murali Mahendra Banala

Murali Mahendra Banala

推荐答案

不处理更改集合的最佳方法是添加一个方法,使用添加的Employee参数添加到集合中。 在此方法中,您将使用NotifyPropertyChanged for MembersCount并将Employee添加到集合中。
The best way not to deal with collection changed is to add a method to add to the collection with a parameter of the Employee being added.  In this method you would use the NotifyPropertyChanged for MembersCount and add the Employee to the collection.


这篇关于如何使用get accessor更新UI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆