The canonical way in WPF to display data is to bind a control to it (see Data Binding in MSDN). This would probably require that you wrap or refactor your messenger class so that it exposes bindable properties. For example, your messenger class might expose a property called MessageText, which you update every time you receive a message:
// INotifyPropertyChanged interface implementation and plumbing
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName);
}
// The property you are going to bind to
private string _messageText = String.Empty;
public string MessageText
{
get { return _messageText; }
set
{
_messageText = value;
OnPropertyChanged("MessageText");
}
}
// How to modify your code to update the bindable property
private void OnMessageReceive(string message) // assuming this method already exists
{
MessageText = MessageText + Environment.NewLine + message;
}
Now you would bind the TextBox.Text property to this new property:
<TextBox Text="{Binding MessageText, Mode=OneWay}" />
This assumes that the messenger object is set as the window's DataContext, e.g. when the window creates the messenger:
public class Window1()
{
_myMessenger = = new DanMessengerClient();
this.DataContext = _myMessenger;
}
Note your messenger class must implement INotifyPropertyChanged for this to work. Also note the OneWay binding so that if the user edits the TextBox it doesn't muck up the MessageText property. (You could also use a TextBlock, so that the user couldn't edit it at all.)
When you've got this set up, WPF will automatically monitor for changes in the _myMessenger.MessageText property, and update the TextBox.Text as they happen (i.e. as messages are received).
Finally, regarding how to do the send: just pass the text:
private void SendButton_Click(...)
{
_myMessenger.Send(MyTextBox.Text);
}
Use the Name attribute to name the text box containing the message to be sent:
<TextBox Name="MyTextBox" />