如何将多个源绑定到ListView? [英] How Do I Bind Multiple sources to ListView?

查看:95
本文介绍了如何将多个源绑定到ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好需要帮助。

我有一个ListView。 ListView绑定到一个收集ex的本地。 Item Source = {Binding Collection1}。在C#代码我得到了这个:ListView.DataContext =对象的名称。这是有效的但是......

事情是,要显示的属性会改变,取决于用户想要看到的内容。

例如用户选择不同的类别我希望ListView显示Properties2而不是Properies1,但ListView的Item Source已经设置为Property1。解决这个问题最简单的方法是什么?

提前感谢您的帮助。

Hi guys need some help please.
I've got a ListView. The ListView is bound to one of the properiets which is a collection ex. Item Source={Binding Collection1} . In C# the code I have got this: ListView.DataContext=name of the object. That is working but...
The thing is that properties that are going to be displayed change depends on what the user wants to see.
For example user chooses differnt category and I want the ListView to display Properties2 not Properies1, but Item Source for the ListView is already set to Property1. What is the simplest way to fix this?
Thanks for help in advance.

推荐答案

我建议使用项目视图模型作为您的对象的包装器希望展示。对象可以实现定义显示数据的方法的接口。然后将视图模型中的属性绑定到视图中显示的项目。此属性将在其getter中调用模型的显示方法。类似的东西。

I would suggest using an item view model as a wrapper for the objects that you wish to display. The objects could implement an interface that defines a method to display the data. Then bind a property in the view model to the items display in the view. This property would call the model’s display method in its getter. Something like.

public   interface IDisplayItem
    {
        string GetDisplayString();
        ..........................
    }

  public  class ItemViewModel
  {
      private readonly IDisplayItem item;
      public ItemViewModel(IDisplayItem obj)
      {
          item = obj;
      }

      public string ItemDisplay
      {
          get
          {
              return item.GetDisplayString();
          }
      }
     ....................................
    }
}

然后,您可以根据需要更改项目集合,而无需更改视图中的绑定。

You can then change the items collection as needed without changing the bindings in the view.


决定构建一个包含两个不同的常规源并将其用作列表视图的源:)
Decided to build one general source which includes two different and use it as a source of the list view:)


这篇关于如何将多个源绑定到ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆