I'm not a smart man. I've spent hours reading a number of different previously asked questions and trying to get this thing to work, but I am still missing something and I'm not sure what it is. I will probably be embarrassed when I realize what it is...but I got the impression from the 2nd link below that making things update any other way really shouldn't be done.
These are the things I have already read:
How do I refresh visual control properties
Refreshing a WPF window on demand
I'm trying to one-way bind a textblock to a string source and have it update automatically as my code runs...but it never seems to update. As for all of the objects I'm using...my original desire with starting to learn C# was to create my own program that could stream video from an input stream of any type over the internet to my phone...obviously I am a long way from that. Your help is greatly appreciated!
XAML
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-
compatibility/2006"
xmlns:c="clr-namespace:WpfApp1"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<c:dataHolder x:Key="source"/>
</Window.Resources>
<Window.DataContext>
<Binding Source="{StaticResource source}"/>
</Window.DataContext>
<Grid>
<TextBox x:Name="tb1" HorizontalAlignment="Left" Height="22"
Margin="45,35,0,0" TextWrapping="Wrap" Text="Enter IP"
VerticalAlignment="Top" Width="195"/>
<Button x:Name="Connect" Content="Connect"
HorizontalAlignment="Left" Margin="390,239,0,0" VerticalAlignment="Top"
Width="75" Click="Connect_Click"/>
<TextBlock x:Name="mblock" Text="{Binding Path=Message, Mode=OneWay,
UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="47"
Margin="45,105,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="195"/>
</Grid>
</Window>
Code Behind
public partial class MainWindow : Window
{
private dataHolder dh;
public MainWindow()
{
dh = new dataHolder();
dh.Message = "Initialize";
InitializeComponent();
}
Binding myBinding = new Binding("myDataProperty");
private void Connect_Click(object sender, RoutedEventArgs e)
{
TcpListener server = null;
try
{
myBinding.Source = dh;
mblock.SetBinding(TextBlock.TextProperty, myBinding);
//Set TcpListener on port 13000.
Int32 port = 13000;
IPAddress localAddr = IPAddress.Parse("192.168.32.137");
server = new TcpListener(localAddr, port);
server.Start();
Byte[] bytes = new Byte[256];
String data = null;
//Enter the listening loop.
while (true)
{
dh.Message = "Waiting for a connection";
TcpClient client = server.AcceptTcpClient();
data = null;
NetworkStream stream = client.GetStream();
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes,0,
i);
dh.Message = "Received:" + data;
data = data.ToUpper();
data = data + "Sucka";
byte[] msg =
System.Text.Encoding.ASCII.GetBytes(data);
stream.Write(msg, 0, msg.Length);
dh.Message = "Sent:" + data;
}
client.Close();
}
}
catch (SocketException ex)
{
string nastyE;
nastyE = ex.Message;
dh.Message = "Socket Exception" + nastyE;
}
finally
{
server.Stop();
}
}
}
dataHolder
public partial class dataHolder : INotifyPropertyChanged
{
private string message;
public string Message
{
get
{
return message;
}
set
{
message = value;
NotifyPropertyChanged("Message");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}