更新 TextBlock 绑定
我正在开发 WP7 应用程序,但在更新绑定到属性的 TextBlock
时遇到了一些问题。我是 MVVM 和 C# 的新手,所以我不确定我做错了什么。
最后我解决了这个问题,但我不明白为什么我的解决方案有效(总是很有趣......),所以我非常感谢您的指导。
在我的应用程序的模型中,我最初有这样的内容:
// 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; }
...
}
}
非常感谢您帮助新手:)
I'm working on a WP7 app and I've had some trouble updating a TextBlock
bound to a property. I'm new to MVVM, and C# in general, so I'm not sure what I'm doing wrong.
In the end I have solved this problem, but I don't understand why my solution works (always fun ...), so I'd really appreciate your guidance.
In my app's Model, I originally had something like this:
// 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
is a enum
, and when it's set by my app, it also sets StatusMessage
too (which is a more human readable description to show the user). My View's TextBlock
is bound to StatusMessage
, but it doesn't update using the above code.
However, if I move NotifyPropertyChanged("StatusMessage")
into Status
, my View's TextBlock
updates like it should. However, I don't understand why this works when the original code above doesn't?
// 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; }
...
}
}
Many thanks in advance for helping a newbie out :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这一行中的问题:
StatusMessage setter 永远不会被调用(NotifyPropertyChanged("StatusMessage") 在那里被调用)
将是正确的调用
可能我的实现是下一个:
Issue in this line:
StatusMessage setter is never called (NotifyPropertyChanged("StatusMessage") called exactly there)
will be the right call
Probably my implementation of this would be next: