在 MVVM 中显示有关集合项的补充信息

发布于 2024-11-27 07:03:47 字数 472 浏览 2 评论 0原文

我对 WPF 和 MVVM 很陌生,并试图了解正常的做事方式。

假设我有一个可观察的客户对象集合。我可以将 ListView 或 ItemsControl 绑定到此,它们都显示正常。

现在假设我有一些有关未存储在客户对象中的每个项目的补充信息。假设可能有特别优惠,并且某些客户有资格享受该优惠,但客户对象中不包含此标志。

在列表中显示此补充信息的标准方法是什么?

我的直接想法是在我的视图模型中创建一些结构的新集合,其中包含客户对象和此标志。然后,我可以将列表直接绑定到该集合,并将各种显示组件绑定到适当的结构成员。

然而,当我已经有一个 ObservableCollection 客户时,这似乎有点浪费。如果我希望新列表“实时”,那么我需要新集合来订阅并对基础客户集合中的添加/删除做出反应。

这是这样做的方法吗?或者有什么方法可以以某种方式单独维护报价标志,但仍然在我的 ListView 中绑定到它们?

非常感谢

new to WPF and MVVM, and trying to get my head around the normal way to do things.

Say I have an observable collection of customer objects. I can bind a ListView, or ItemsControl to this and they all display fine.

Now say I have some supplementary information about each item that is not stored in the customer object. Say perhaps a special offer is on, and certain customers are eligible for the offer, but this flag is not held in the customer object.

What is the standard way to display this supplementary info in the list?

My immediate thought is to create a new collection of some structure in my viewmodel that contains both a customer object, and this flag. I can then bind the list straight to this collection, and bind the various display components to the appropriate struct members.

However, this seems a bit of a waste when I already have an ObservableCollection of customers. If I want the new list to be "live", then I need the new collection to subscribe and react to add/removes in the underlying customer collection.

Is this the way to do it? Or is there some way I can maintain the offer flags in some way separately, and yet still bind to them in my ListView?

Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

找个人就嫁了吧 2024-12-04 07:03:47

当使用 MVVM 模式时,ViewModel 是“视图的模型”,即它是与当前视图非常相似的模型。

我明白为什么您可能希望底层模型分别存储报价和客户。

伪代码:

class MyModel
{
   List<Offer> Offers {get; set;}
   List<Customer> Customers {get; set;}
}

但是,您无法将 ListView 绑定到您的客户并以某种方式将它们与您的报价相关联。 ViewModel 的作用是塑造模型,以便更轻松地将其绑定到视图:

class CustomerViewModel
{
    // the customer model
    private Customer customer;

    // and offer - might be null
    private Offer offer;

    // expose properties of the two above model objects here
} 

您的视图模型将创建这些 CustomerViewModel 实例的 ObservableCollection,使用模型 Customer 实例的集合,查找 Offer(如果存在)。

ObservableCollection 可以轻松绑定到您的视图。

When using the MVVM pattern, the ViewModel is the "model of your view", i.e. it is a model that closely resembles your current view.

I can see why you might want you underlying model to store offers and customers separately.

Pseudocode:

class MyModel
{
   List<Offer> Offers {get; set;}
   List<Customer> Customers {get; set;}
}

However, you cannot bind a ListView to your customers and somehow correlate them with your offers. It is the role of your ViewModel to shape the Model so that it is easier to bind it to your view:

class CustomerViewModel
{
    // the customer model
    private Customer customer;

    // and offer - might be null
    private Offer offer;

    // expose properties of the two above model objects here
} 

Your view model would create an ObservableCollection of these CustomerViewModel instances, using the collection of model Customer instances, locating an Offer if one exists.

This ObservableCollection<CustomerViewModel> can be readily bound to your view.

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