WPF Observablecollection主细节场景

发布于 2024-08-17 06:45:32 字数 854 浏览 6 评论 0原文

对此有任何帮助都会很棒。

我有一个模型,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彼岸花ソ最美的依靠 2024-08-24 06:45:32

我对 WCF 的了解不够多,无法说明是否可以将其配置为以简单数组以外的其他类型返回嵌套集合,但我会尝试给出 WPF 的视角。

这里没有魔法,简单的数组不实现任何类型的更改通知;您只需将其包装在某种视图感知集合中,例如 ObservableCollection。

将所有内容包装起来并仅与包装的集合进行交互,例如:

public class MasterWrapper
{
...
    public MasterWrapper(Master master)
    {
        this.Details = new ObservableCollection<Detail>();
        this.Master = master;
        foreach (var detail in Master.Details)
            this.Details.Add(detail);
    }
...

    public static ObservableCollection<MasterWrapper> GetMasters()
    {
        ObservableCollection<MasterWrapper> results = 
            new ObservableCollection<MasterWrapper>();
        List<Master> modelMasters = null; // Populate this from the service.
        foreach (var m in modelMasters)
            results.Add(new MasterWrapper(m));
        return results;
    }

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:

public class MasterWrapper
{
...
    public MasterWrapper(Master master)
    {
        this.Details = new ObservableCollection<Detail>();
        this.Master = master;
        foreach (var detail in Master.Details)
            this.Details.Add(detail);
    }
...

    public static ObservableCollection<MasterWrapper> GetMasters()
    {
        ObservableCollection<MasterWrapper> results = 
            new ObservableCollection<MasterWrapper>();
        List<Master> modelMasters = null; // Populate this from the service.
        foreach (var m in modelMasters)
            results.Add(new MasterWrapper(m));
        return results;
    }
十年不长 2024-08-24 06:45:32

你是对的,问题是Details属性没有将更改通知回视图,因为它只是一个Detail对象数组...

我会将其设为ObservableCollection并在 Master 的构造函数中加载集合,就像...

public Master(Detail[] details)
{
    Details = new ObservableCollection<Detail>(details);
}

记住 ObservableCollection 采用 IEnumerableList 作为其构造函数中的参数。

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 in Master's constructor, like...

public Master(Detail[] details)
{
    Details = new ObservableCollection<Detail>(details);
}

remember that ObservableCollection<T> takes IEnumerable<T> or List<T> as a parameter in its constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文