Hello everyone! I'm quite a novice with C# and a complete newbie regarding WPF. Probably it's a very basic question and piece of cake for the pros. Please bear with me.
I need to display a dynamic textblock with text changed in run time without additional triggers such as button clicks and so. For some reason (my insufficient understanding of the concept obviously) textblock stays empty.
Xaml is as simple as it can be:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<TextBlock Text="{Binding Path=Name}"/>
</Grid>
</Window>
And the code behind it simplified as well:
using System.ComponentModel;
using System.Threading;
using System.Windows;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Client client = new Client();
client.Name = "Michael";
Thread.Sleep(1000);
client.Name = "Johnson";
}
}
public class Client : INotifyPropertyChanged
{
private string name = "The name is:";
public event PropertyChangedEventHandler PropertyChanged;
public string Name
{
get
{
return this.name;
}
set
{
if (this.name == value)
return;
this.name = value;
this.OnPropertyChanged(new PropertyChangedEventArgs("Name"));
}
}
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, e);
}
}
}
Thanks in advance,
Ceres