我建议列表框绑定到一个CompositeCollection那你在代码中建立。 在这个例子中,我使用了一个ViewModel,但是你也可以在代码隐藏方面做同样的事情。 你可以找到很多关于如何通过谷歌为ViewModel实现ViewModelBase和DelegateCommand的例子。
下面是这个例子的分解:
- 这个例子加载客户和Person对象分为二的ObservableCollection容器支持修改的集合。
- 列表框结合其的ItemsSource到CompositeCollection(ObjectCollection),其包含两个ObservableCollections。
- 列表框也结合其的SelectedItem到对象(SelectedObject)支持两个基本类型。
- 该按钮添加一个新的人来告诉你可以修改CompositeCollection。
- 我在最后的完整性添加客户和个人defintitions。
这里查看:
<Window x:Class="StackOverflow.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Main Window" Height="400" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ListBox Grid.Row="0"
SelectedItem="{Binding Path=SelectedObject}"
ItemsSource="{Binding Path=ObjectCollection}">
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding FirstName}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Grid.Row="1" Content="Add Person" Command="{Binding Path=AddPerson}"/>
</Grid>
</Window>
这里是视图模型:
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")
});
}
}
在代码背后/ VM中构建CompositeCollection的优势是什么,而不是在XAML中使用绑定到VM各个属性的CollectionContainer来实现? – SOReader 2013-11-27 13:21:06