New to WPF, how to update a TextBlock on MainWindow?



Although I have been developing ASP.NET for many years, I have never seriously done anything in Windows Form or WPF development.

I have a MainWindow.xaml as the world's simplest chat server, which has a TextBlock control, shown below.

<Window x:Class="WpfApplication1.MainWindow"
Title="MainWindow" Height="350" Width="525">
<TextBlock Height="137" HorizontalAlignment="Left" Margin="57,96,0,0" Name="TextBlock1" Text="TextBlock" VerticalAlignment="Top" Width="272" />

I have a Server.cs file, which has the following code:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Server : IService
public string Ping(string name)
//Console.WriteLine("Client Name: ('{0}')", name);
//Of course the following line does not work!
MainWindow.TextBlock1.Text = "Received client message.";
return "Hello, " + name;

The commented out code:
//Console.WriteLine("Client Name: ('{0}')", name);
tells you that I am modifying a console-based demo chat server, and would like to provide a GUI such that the Ping method will update TextBlock1 in MainWindow and return a string to the client.

I am wondering how I can access the TextBlock1 control on MainWindow from Server.cs.

Does Server.cs have to raise an event and let MainWindow subscribe to it like in ASP.NET?

You see I have no clue how Windows client application works. I've skimmed through Sams Teach Yourself WPF in 24 Hours, but can't find such information. Please give me a hint or a reference to get started. Thanks.

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question