问题描述
大家好,
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
推荐答案
这篇关于如何使用get accessor更新UI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!