我建议使用代码生成一个CompositeCollection并将ListBox绑定到它上面。在这个例子中,我使用了ViewModel,但你也可以在代码后台完成同样的操作。你可以通过Google搜索,在许多示例中找到如何实现ViewModelBase和DelegateCommand用于ViewModel的方法。
这是此示例的详细信息:
- 此示例将Customer和Person对象加载到两个ObservableCollection容器中以支持修改集合。
- ListBox将其ItemsSource绑定到CompositeCollection(ObjectCollection),该CompositeCollection包含两个ObservableCollections。
- ListBox还将其SelectedItem绑定到一个对象(SelectedObject)以支持两种基本类型。
- 按钮添加了一个新的Person以展示如何修改CompositeCollection。
- 我在末尾添加了Customer和Person的定义以完整表示。
以下是视图:
<Window x:Class="StackOverflow.Views.MainView"
xmlns="http:
xmlns:x="http:
Title="Main Window" Height="400" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ListBox Grid.Row="0"
SelectedItem=""
ItemsSource="">
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Grid.Row="1" Content="Add Person" Command=""/>
</Grid>
</Window>
这是 ViewModel:
using System.Collections.Generic;
using System.Windows.Data;
using System.Windows.Input;
using ContextMenuNotFiring.Commands;
using ContextMenuNotFiring.Models;
namespace StackOverflow.ViewModels
{
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
AddPerson = new DelegateCommand<object>(OnAddPerson, CanAddPerson);
CollectionContainer customers = new CollectionContainer();
customers.Collection = Customer.GetSampleCustomerList();
CollectionContainer persons = new CollectionContainer();
persons.Collection = Person.GetSamplePersonList();
_oc.Add(customers);
_oc.Add(persons);
}
private CompositeCollection _oc = new CompositeCollection();
public CompositeCollection ObjectCollection
{
get { return _oc; }
}
private object _so = null;
public object SelectedObject
{
get { return _so; }
set
{
_so = value;
}
}
public ICommand AddPerson { get; set; }
private void OnAddPerson(object obj)
{
CollectionContainer ccItems = _oc[1] as CollectionContainer;
if ( ccItems != null )
{
ObservableCollection<Person> items = ccItems.Collection as ObservableCollection<Person>;
if (items != null)
{
Person p = new Person("AAAA", "BBBB");
items.Add(p);
}
}
}
private bool CanAddPerson(object obj)
{
return true;
}
}
}
以下是模型:
public class Customer
{
public String FirstName { get; set; }
public String LastName { get; set; }
public Customer(String firstName, String lastName)
{
this.FirstName = firstName;
this.LastName = lastName;
}
public static ObservableCollection<Customer> GetSampleCustomerList()
{
return new ObservableCollection<Customer>(new Customer[4] {
new Customer("Charlie", "Zero"),
new Customer("Cathrine", "One"),
new Customer("Candy", "Two"),
new Customer("Cammy", "Three")
});
}
}
public class Person
{
public String FirstName { get; set; }
public String LastName { get; set; }
public Person(String firstName, String lastName)
{
this.FirstName = firstName;
this.LastName = lastName;
}
public static ObservableCollection<Person> GetSamplePersonList()
{
return new ObservableCollection<Person>(new Person[4] {
new Person("Bob", "Smith"),
new Person("Barry", "Jones"),
new Person("Belinda", "Red"),
new Person("Benny", "Hope")
});
}
}