简体   繁体   English

如何将WPF中的列表框绑定到通用列表?

[英]How to Bind Listbox in WPF to a generic list?

i'm having trouble getting a clear answer for this. 我无法为此获得明确的答案。 I have a Static class (DataHolder) that holds a static list with a complex type (CustomerName and CustomerID properties). 我有一个静态类(DataHolder),它包含一个复杂类型的静态列表(CustomerName和CustomerID属性)。 I want to bind it to a ListBox in WPF but add another item that will have the word "All" for future drag and drop capablilities. 我想将它绑定到WPF中的ListBox,但添加另一个将具有单词“All”的项目,以用于将来的拖放功能。 Anyone? 任何人?

Create a ViewModel Class you can databind to! 创建一个可以数据绑定到的ViewModel类! The ViewModel can reference the static class and copy the items to its own collection and add the all item to it. ViewModel可以引用静态类并将项目复制到其自己的集合中,并将所有项目添加到其中。

Like this 像这样

public class YourViewModel
{
        public virtual ObservableCollection<YourComplexType> YourCollection
        {
            get
            {
                var list = new ObservableCollection<YourComplexType>(YourStaticClass.YourList);
                var allEntity = new YourComplexType();

                allEntity.Name = "all";
                allEntity.Id = 0;

                list.Insert(0, allEntity);

                return list;
            }

        }
}

Note, sometimes, you need empty Items. 请注意,有时您需要空项目。 Since WPF can't databind to null values you need to use the same approach to handle it. 由于WPF无法数据绑定为空值,因此您需要使用相同的方法来处理它。 The empty business entity has been a best practice for it. 空业务实体是最佳实践。 Just google it. 只是谷歌吧。

That "All" item has to be part of the list you bind your ListBox against. “All”项必须是绑定ListBox的列表的一部分。 Natuarally you can not add that item to the DataHolder list because it holds items of type Customer (or similar). 通常,您无法将该项添加到DataHolder列表,因为它包含Customer(或类似)类型的项。 You could of course add a "magic" Customer that always acts as the "All" item but that is for obvious reasons a serious case of design smell (it is a list of Customers after all). 你当然可以添加一个“神奇”的客户,它总是充当“全部”项目,但这显然是一个严重的设计气味案例(毕竟它是一个客户列表)。

What you could do, is to not bind against the DataHolder list directly but introduce a wrapper. 你可以做的是不直接绑定DataHolder列表,而是引入一个包装器。 This wrapper would be your ViewModel. 这个包装器将是你的ViewModel。 You would bind your ListBox agains a list of CustomerListItemViewModel that represents either a Customer or the "All" item. 您将再次将ListBox绑定到CustomerListItemViewModel列表,该列表表示Customer或“All”项。

CustomerViewModel
{
    string Id { get; private set; }
    string Name { get; set; }
    public static readonly CustomerViewModel All { get; private set; }

    static CustomerViewModel()
    {
       // set up the one and only "All" item
       All = new CustomerViewModel();
       All.Name = ResourceStrings.All;
    }


    private CustomerViewModel()
    {
    }

    public CustomerViewModel(Customer actualCustomer)
    {
        this.Name = actualCustomer.Name;
        this.Id = actualCustomer.Id;
    }
}

someOtherViewModel.Customers = new ObservableCollection<CustomerViewModel>();
// add all the wrapping CustomerViewModel instances to the collection
someOtherViewModel.Customers.Add(CustomerViewModel.All);

And then in your Drag&Drop code somewhere in the ViewModel: 然后在ViewModel中的某个拖放代码中:

if(tragetCustomerViewModelItem = CustomerViewModel.All)
{
     // something was dropped to the "All" item
}

I might have just introduced you to the benefits of MVVM in WPF. 我可能刚刚向您介绍了WPF中MVVM的优点。 It saves you a lot of hassle in the long run. 从长远来看,它可以为您节省很多麻烦。

If you use binding than the data provided as the source has to hold all of the items, ie. 如果您使用绑定而不是提供的数据,因为源必须包含所有项目,即。 you can't databind and then add another item to the list. 你不能数据绑定,然后添加另一个项目到列表中。

You should add the "All" item to the DataHolder collection, and handle the 'All' item separately in your code. 您应该将“All”项添加到DataHolder集合中,并在代码中单独处理“All”项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM