[英]C# binding ObservableCollection<T> with List<T>
I'm making an API which is will be use my chat program. 我正在制作一个将用于我的聊天程序的API。
API core class have a room list like a List<Room> Rooms_;
API核心类具有一个房间列表,例如
List<Room> Rooms_;
API core class have a property. API核心类具有一个属性。
public Room[] Rooms { get { return Rooms_.ToArray(); } }
and API core class also have a property which is easily to print room list. API核心类也具有易于打印房间列表的属性。 This property is
public ObservableCollection<Room> ObservableRooms { get { return new ObservableCollection<Room>(Rooms_); }
此属性是
public ObservableCollection<Room> ObservableRooms { get { return new ObservableCollection<Room>(Rooms_); }
public ObservableCollection<Room> ObservableRooms { get { return new ObservableCollection<Room>(Rooms_); }
Now, when room is added or removed, i coded Add
function and Remove
function like this. 现在,当添加或删除房间时,我
Add
功能和Remove
功能进行了编码。
Rooms_.Add(new_room);
Rooms_.Remove(room);
but the ObservableRooms
dosen't change automatically. 但是
ObservableRooms
不会自动更改。
I want to bind ObservableRooms
property to the Rooms_
list. 我想将
ObservableRooms
属性绑定到Rooms_
列表。
Anyone know this? 有人知道吗?
Thanks. 谢谢。
If you are binding directly to the ObservableCollection why not manipulate the ObservableCollection directly. 如果直接绑定到ObservableCollection,那么为什么不直接操作ObservableCollection。
private ObservableCollection<Room> _observableRooms
public ObservableCollection<Room> ObservableRooms
{
get { return _observableRooms; }
set { _observableRooms = value; }
}
Then add and remove directly from ObservableRooms and your UI should update correctly. 然后直接从ObservableRooms添加和删除,您的UI应该正确更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.