Now that our we've used Expression Blend to databind our
control UI, and to tweak and polish the interactivity of the UI, let's go back
to Visual Studio and write the code that implements the UI chat behavior
Specifically, we'll add the below code to our Page
constructor to initiate a ChatSession with a remote user, and then handle the
scenario where the "Send" button is clicked to send a message to the
When we add the above code and re-run the application we'll
see that our UI now databinds to a ChatSession with "ScottGu" as the
RemoteUserName (instead of the fake design-time data we defined earlier).
When we type text in the message TextBox and click the customized Send button
our Listbox is automatically updated with the chat history:
Why did the ListBox automatically update you might
wonder? It did this because the ListBox was data-bound to the ChatSession.MessageHistory
property - which is of type ObservableCollection<ChatMessage>. This
means the collection automatically raises change notifications when a new
ChatMessage object is added to it, which the ListBox then detects and uses to update
itself with the new data.
No explicit code was required by us to have the ListBox
reflect these changes. The clean view/model binding architecture of our
application automatically handled it for us.