I'm making chatting program and I have an API for connect to chat server.
in API here is main class.
public partial class PengChat3ClientSock : IDisposable
well, I overrided ToString
method too.
public override string ToString()
{
string s;
if (ConnectedIP != null && ConnectedPort != null)
s = ConnectedIP + ':' + ConnectedPort.Value.ToString();
else
return "";
if (string.IsNullOrEmpty(Nickname))
return s;
else
return s + " \"" + Nickname + "\"";
}
Well, in client program,
this is a list of sockets
private List<PengChat3ClientSock> socketList = new List<PengChat3ClientSock>();
And i have combobox which is to showing connection list
<ComboBox Grid.Row="0" Grid.ColumnSpan="2" Height="20">
</ComboBox>
Now, i want to bind ComboBox ItemsSource with variable socketList
I want to add/remove socket list when client create a new socket, and push back to the socketList or remove from the socketList.
It is possible?
Thanks.
Use an ObservableCollection<PengChang3ClientSock>
instead of a plain list. The ObservableCollection will raise and CollectionChangedEvent when items are added or removed from the collection to notify bound controls.
Set the ObservableCollection as ItemsSource of the ComboBox like
<ComboBox Grid.Row="0" Grid.ColumnSpan="2" Height="20" ItemsSource="{Binding Sockets}">
</ComboBox>
This setup requires that the DataContext (ViewModel) of the ComboBox has a property Sockets encapsulating your ObservableCollection.
public ObservableCollection Sockets{
get{
return socketList;
}
}
You should use ObservableCollection.
private ObservableCollection<PengChat3ClientSock> socketList = new ObservableCollection<PengChat3ClientSock>();
Then name combobox in XAML
<ComboBox Name="SocketListComboBox" Grid.Row="0" Grid.ColumnSpan="2" Height="20">
</ComboBox>
And in code (xaml.cs), in constructor just after InitializeComponents(); do
SocketListComboBox.ItemsSource = socketList;
Other way is to bind it in XAML;
<ComboBox Name="SocketListComboBox" Grid.Row="0" Grid.ColumnSpan="2" Height="20" ItemsSource = "{Binding socketList}">
</ComboBox>
In constructor set ComboBox DataContext to owner of socketList; And socketList must be a property with public get.
public ObservableCollection<PengChat3ClientSock> socketList {get; protected set; }
Yes you can. You should hold the list of sockets in an ObservableCollection
private ObservableCollection]<PengChat3ClientSock> socketList = new List<PengChat3ClientSock>();
Then add a binding to the combobox like this:
ComboBox Grid.Row="0" Grid.ColumnSpan="2" Height="20" ItemSource="{Binding socketList}>
The last thing you need to do is to set the DataContext of the Window with the ComboBox to the class which has the list.
DataContext = new ClassWithSocketList();
From now on every action you'll make on the list would have binding to the list.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.