WPF InotifyPropertyChanged 和视图模型
所以我认为我正在做一些非常基本的事情。我知道为什么这不起作用,但似乎应该有一种直接的方法可以使其发挥作用。
代码:
private string fooImageRoot;
// ....
public BitmapImage FooImage
{
get
{
URI imageURI = new URI(Path.Combine(fooImageRoot, CurrentFooTypes.FooObject.FooImageName));
return imageURI;
}
}
所以CurrentFOoTypes和FooObject也支持INotifyPropertyChanged。
因此,如果我将 TextBlock 绑定到 CurrentFooTypes.FooObject.FooImageName,如果 fooObject 或 FooImageName 更改文本块更新。我如何订阅我的视图模型对象以类似的方式接收更新。
So I think I'm doing something pretty basic. I know why this doesn't work, but it seems like there should be a straight foward way to make it work.
code:
private string fooImageRoot;
// ....
public BitmapImage FooImage
{
get
{
URI imageURI = new URI(Path.Combine(fooImageRoot, CurrentFooTypes.FooObject.FooImageName));
return imageURI;
}
}
So CurrentFOoTypes and FooObject also supports INotifyPropertyChanged.
So If I bind a TextBlock to CurrentFooTypes.FooObject.FooImageName, if either fooObject or FooImageName change the textblock updates. How can I subscribe my viewmodel object to recieve updates in a similiar fasion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我错了请纠正我。您希望收到 FooImageName 和 FooObject 属性更改的通知,这两个对象都使用 INotifyPropertyChanged 来提醒观察者这些属性已更改。
约什·史密斯 (Josh Smith) 表现不错 文章,他介绍了一个 PropertyObserver 对象,该对象仅用于此场景。
MVVM Foundation 包含此对象以及其他对 MVVM 开发有用的对象。
您可以使用 PropertyObserver 或自定义代码来监视您感兴趣的属性(在本例中为 FooObject 和 FooImageName)的更改,并根据这些更改执行更新图像 URI 所需的任何操作。
Correct me if I'm wrong. You want to be notified of changes to the properties FooImageName and FooObject, both owning objects use INotifyPropertyChanged to alert observers that these properties have changed.
Josh Smith had a nice article where he introduced a PropertyObserver object, which is used for just this scenario.
The MVVM Foundation includes this object as well as other helpful objects for MVVM development.
You can use the PropertyObserver, or custom code, to watch for changes in the properties you're interested in (in this case FooObject and FooImageName) and perform whatever actions you need to update the image URI based on those changes.