Reputation: 131
I'm trying to specify data variable for my TextBlock at xaml:
<TextBlock Name="Test11" Text="{Binding Path=Test}"></TextBlock>
I'm using OnPropertyChanged for it:
public partial class MainWindow : Window, INotifyPropertyChanged
private string _test;
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
public string Test
{
get
{
return _test;
}
set
{
_test = value;
OnPropertyChanged("Test");
}
}
And trying to set value at MainWindow constructor:
public MainWindow()
{
InitializeComponent();
Test = "teeest";
}
But Textblock.Text wasn't updated... What I'm doing wrong?
Upvotes: 1
Views: 6329
Reputation: 11896
In the Window constructor after
InitializeComponents()
put
this.DataContext = this;
Upvotes: 0
Reputation: 12766
You need to set the datacontext so the UI knows where to get the data for the binding.
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
Test = "teeest";
}
Upvotes: 2