简体   繁体   English

C#绑定ObservableCollection <T> 与清单 <T>

[英]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.

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