【发布时间】:2015-04-23 07:14:16
【问题描述】:
我已经阅读了几篇关于此的文章,但似乎无法在我的属性更改时更新我的标签。 PropertyChanged 事件正在触发,并且属性正在更新为新文本,但标签未更新。 感谢您的帮助!
XAML
<Grid.Resources>
<c:UserInformation x:Key="myTaskData"/>
</Grid.Resources>
<Label Name="lblTaskNameTitle" Content="{Binding Path=propTaskName, UpdateSourceTrigger=PropertyChanged}"/>
窗口
UserInformation t = new UserInformation();
t.propTaskName = "Updated Task Name";
this.DataContext = t.propTaskName;
代码背后
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));
}
}
更新以反映评论建议。
【问题讨论】:
-
你在哪里设置
myTaskData?看起来应该是你的DataContext而不是任务名称。 -
myTaskData 在 Grid.Resources 部分中设置。