WPF Observablecollection主细节场景
对此有任何帮助都会很棒。
我有一个模型,
public class Master
{
...
public Detail[] Details {get; set;}
}
我正在从 WCF 服务填充我的视图模型,该服务返回我的主对象集合。我已将服务引用配置为返回 observablecollection,以便我可以在视图模型中轻松使用它。
然后,我的视图模型有
public ObservableCollection<Master> Masters {get; set;}
public Master SelectedMaster {get; set;}
在我看来,我有 2 个列表框 - 一个绑定到我的 Masters 属性,另一个绑定到 SelectedMaster.Details。
除了当我尝试向 SelectedMaster 添加新细节时,这一切都工作正常。
SelectedMaster 中的Details 集合只是一个Details 列表(不是ObservableCollection),这就是明显的原因。
我在这里有什么选择?我尝试过实现 INotifyPropertyChanged 但这似乎不起作用。我可以有另一个用于详细信息的 ObservableCollection,但这意味着当 SelectedMaster 更改时我必须保持此集合同步(SelectedMaster 属性绑定到我的第一个列表框中的 SelectedItem。
希望这一切顺利。真的很喜欢一些反馈。如果 WCF 能够像处理 Masters 集合一样将详细信息集合作为可观察集合返回,那就太理想了,但它似乎不是这样工作的,
谢谢。
any help with this would be great.
I have a model
public class Master
{
...
public Detail[] Details {get; set;}
}
I am populating my view model from a WCF service which returns my collection of Master objects. I have configured the service reference to return observablecollection so I can use it easily in my view model.
My view model then has
public ObservableCollection<Master> Masters {get; set;}
public Master SelectedMaster {get; set;}
In my view I have 2 listboxes - one bound to my Masters property and the other bound to SelectedMaster.Details.
This all works fine apart from when I add try to add a new detail to the SelectedMaster.
The collection of Details in the SelectedMaster is just a list of Details (not an ObservableCollection) which is clearly why.
What options do I have here? I have tried implementing INotifyPropertyChanged but that doesn't seem to work. I could have another ObservableCollection for the Details but that means I have to keep this collection in sync when the SelectedMaster is changed (the SelectedMaster property is bound to the SelectedItem on my first listbox.
Hope this comes across OK. Would really love some feedback. Would be ideal if WCF could just return the collection of details as an observablecollection as it does with the collection of Masters but it doesn't seem to work like that.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对 WCF 的了解不够多,无法说明是否可以将其配置为以简单数组以外的其他类型返回嵌套集合,但我会尝试给出 WPF 的视角。
这里没有魔法,简单的数组不实现任何类型的更改通知;您只需将其包装在某种视图感知集合中,例如 ObservableCollection。
将所有内容包装起来并仅与包装的集合进行交互,例如:
I don't know enough WCF to say whether it can be configured to return nested collections as other types than simple arrays, but I'll try to give the WPF perspective.
There's no magic here, a simple array doesn't implement any kind of change notification; you simply have to wrap it in some kind of view-aware collection such as
ObservableCollection
.Wrap everything up and only interact with the wrapped collections, example:
你是对的,问题是Details属性没有将更改通知回视图,因为它只是一个Detail对象数组...
我会将其设为
ObservableCollection
并在 Master 的构造函数中加载集合,就像...记住
ObservableCollection
采用IEnumerable
或List
作为其构造函数中的参数。You're right that the issue is that the Details property does not have changes notified back to the View, because it is just an array of Detail objects...
I would make it an
ObservableCollection<Detail>
and load up the collection inMaster
's constructor, like...remember that
ObservableCollection<T>
takesIEnumerable<T>
orList<T>
as a parameter in its constructor.