参考:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2057951&SiteID=1
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2551491&SiteID=1
如下:
<Window x:Class="LearningSolution.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:LearningSolution">
<Window.Resources>
<local:CustomerCollection x:Key="Customers"/>
</Window.Resources>
<StackPanel>
<ListView Width="600" Height="400"
ItemsSource="{Binding Source={StaticResource Customers}}">
<ListView.View>
<GridView>
<GridViewColumn Width="100" Header="Customer ID"
DisplayMemberBinding="{Binding Id}"/>
<GridViewColumn Width="100" Header="Customer Name"
DisplayMemberBinding="{Binding Name}"/>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</Window>
public class Customer : INotifyPropertyChanged
{
private string id, name;
public string Id
{
get { return this.id; }
set
{
if (value != this.id)
{
this.id = value;
OnPropertyChanged(new PropertyChangedEventArgs("Id"));
}
}
}
public string Name
{
get { return this.name; }
set
{
if (value != this.name)
{
this.name = value;
OnPropertyChanged(new PropertyChangedEventArgs("Name"));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (this.PropertyChanged != null) this.PropertyChanged(this, e);
}
}
public class CustomerCollection : ObservableCollection<Customer>
{
public CustomerCollection()
{
this.Add(new Customer() { Id = "C-111", Name = "Mike" });
this.Add(new Customer() { Id = "C-222", Name = "Bigger" });
this.Add(new Customer() { Id = "C-333", Name = "Pop" });
}
}
http://msdn2.microsoft.com/en-us/library/ms752213.aspx
http://msdn2.microsoft.com/en-us/library/ms748988.aspx