Joy Hyuk Lee
Joy Hyuk Lee

Reputation: 776

C# binding ObservableCollection<T> with List<T>

I'm making an API which is will be use my chat program.

API core class have a room list like a List<Room> Rooms_;

API core class have a property. public Room[] Rooms { get { return Rooms_.ToArray(); } }

and API core class also have a property which is easily to print room list. This property is 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.

Rooms_.Add(new_room);
Rooms_.Remove(room);

but the ObservableRooms dosen't change automatically.

I want to bind ObservableRooms property to the Rooms_ list.

Anyone know this?

Thanks.

Upvotes: 0

Views: 105

Answers (1)

nickm
nickm

Reputation: 1775

If you are binding directly to the ObservableCollection why not manipulate the ObservableCollection directly.

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.

Upvotes: 1

Related Questions