Reputation: 86
I have read several articles about this and can't quite seem to get my label to update when my property is changed. The PropertyChanged event is firing, and the property is updating to the new text but the label is not updating. Thanks for the help!
XAML
<Grid.Resources>
<c:UserInformation x:Key="myTaskData"/>
</Grid.Resources>
<Label Name="lblTaskNameTitle" Content="{Binding Path=propTaskName, UpdateSourceTrigger=PropertyChanged}"/>
Window
UserInformation t = new UserInformation();
t.propTaskName = "Updated Task Name";
this.DataContext = t.propTaskName;
Code Behind
class UserInformation : INotifyPropertyChanged
{
private string strTaskName = "Task Name: ";
public string propTaskName
{
get { return this.strTaskName; }
set
{
this.strTaskName = value;
NotifyPropertyChanged("propTaskName"); //Call the method to set off the PropertyChanged event.
}
}
//INotifyPropertyChanged stuff
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
Updated to reflect comment suggestions.
Upvotes: 0
Views: 12750
Reputation: 2516
Bindings look at the DataContext, not the code behind. You can make them the same thing by adding DataContext = this to your code behind, although you would want to use a separate class.
Upvotes: 1
Reputation: 513
Xaml should be:
<Label Name="lblTaskNameTitle" Content="{Binding Path=propTaskName, UpdateSourceTrigger=PropertyChanged}"/>
UserInformation t = new UserInformation();
t.propTaskName = "Updated Task Name";
this.DataContext = t;
Upvotes: 3