我已經閱讀了幾篇關於這方面的文章,並且看起來不太可能讓我的標籤在我的屬性發生更改時進行更新。 PropertyChanged事件觸發,並且屬性正在更新爲新文本,但標籤未更新。 感謝您的幫助!WPF綁定標籤內容到屬性
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部分中設置。 – Litmas