我正在WP7應用程序中工作,並且在更新綁定到屬性的TextBlock
時遇到了一些問題。我是MVVM和C#的新手,因此我不確定自己做錯了什麼。更新TextBlock綁定
最後我已經解決了這個問題,但我不明白爲什麼我的解決方案能夠工作(總是很有趣...),所以我非常感謝您的指導。
在我的應用程序的模型,我本來是這樣的:
// Broken
namespace MyApp.MyModel
{
public class MetaData : INotifyPropertyChanged
{
private StatusType status;
public StatusType Status
{
get { return status; }
set
{
status = value;
statusMessage = ConvertStatusToSomethingMeaningful(value);
}
}
private string statusMessage;
public string StatusMessage
{
get { return statusMessage; }
private set
{
statusMessage = value;
// This doesn't work
NotifyPropertyChanged("StatusMessage");
}
}
...
}
}
Status
是enum
,當它是由我的應用程序設置,還設置StatusMessage
太(這是一個更可讀的描述顯示用戶)。我的視圖的TextBlock
綁定到StatusMessage
,但它不會使用上面的代碼進行更新。
但是,如果我將NotifyPropertyChanged("StatusMessage")
移動到Status
,我的查看的TextBlock
更新應該如此。但是,我不明白爲什麼這個原理上面的代碼不工作?
// Fixed
namespace MyApp.MyModel
{
public class MetaData : INotifyPropertyChanged
{
private StatusType status;
public StatusType Status
{
get { return status; }
set
{
status = value;
StatusMessage = ConvertStatusToSomethingMeaningful(value);
// This works
NotifyPropertyChanged("StatusMessage");
}
}
public string StatusMessage { get; private set; }
...
}
}
提前非常感謝在這一行幫助一個新手了:)
,你能否告訴我們'ConvertStatusToSomethingMeaningful'和'XAML'? –