具体方案为:将ItermsSource设置为实现 INotifyCollectionChanged 接口的对象,以使集合的更改在 ItemsControl 中反映出来。
ObservableCollection<T> 类即定义这样的对象,不要贪图方便而使用List<T>。
请看Listbox 添加删除演示
XAML:
<
UserControl
x:Class
="ListBoxDemo.MainPage"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable ="d"
d:DesignHeight ="300" d:DesignWidth ="400" >
< Grid x:Name ="LayoutRoot" Background ="White" >
< ListBox Height ="200" Width ="200" x:Name ="listBox" ItemsSource =" {Binding} " DisplayMemberPath ="Name" ></ ListBox >
< Button Content ="增加" Height ="23" HorizontalAlignment ="Left" Margin ="11,270,0,0" Name ="buttonAdd" VerticalAlignment ="Top" Width ="75" Click ="buttonAdd_Click" />
< Button Content ="删除" Height ="23" HorizontalAlignment ="Left" Margin ="100,270,0,0" Name ="buttonRemove" VerticalAlignment ="Top" Width ="75" Click ="buttonRemove_Click" />
</ Grid >
</ UserControl >
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable ="d"
d:DesignHeight ="300" d:DesignWidth ="400" >
< Grid x:Name ="LayoutRoot" Background ="White" >
< ListBox Height ="200" Width ="200" x:Name ="listBox" ItemsSource =" {Binding} " DisplayMemberPath ="Name" ></ ListBox >
< Button Content ="增加" Height ="23" HorizontalAlignment ="Left" Margin ="11,270,0,0" Name ="buttonAdd" VerticalAlignment ="Top" Width ="75" Click ="buttonAdd_Click" />
< Button Content ="删除" Height ="23" HorizontalAlignment ="Left" Margin ="100,270,0,0" Name ="buttonRemove" VerticalAlignment ="Top" Width ="75" Click ="buttonRemove_Click" />
</ Grid >
</ UserControl >
CS:
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
namespace ListBoxDemo
{
public partial class MainPage : UserControl
{
public MainPage ()
{
InitializeComponent ();
BindingData ();
}
ObservableCollection<User> myUsers = new ObservableCollection<User> (); // 数据源
private void BindingData ()
{
myUsers.Clear ();
myUsers.Add ( new User ( " A1 ", " A11 "));
myUsers.Add ( new User ( " A2 ", " A12 "));
myUsers.Add ( new User ( " A3 ", " A13 "));
myUsers.Add ( new User ( " A4 ", " A14 "));
myUsers.Add ( new User ( " A5 ", " A15 "));
this.listBox.ItemsSource = myUsers; // 绑定数据源
}
private void buttonAdd_Click ( object sender, RoutedEventArgs e)
{
myUsers.Add ( new User ( " A " + (myUsers.Count + 1), " A1 " + myUsers.Count + 1));
// this.listBox.ItemsSource = myUsers;
}
private void buttonRemove_Click ( object sender, RoutedEventArgs e)
{
if (myUsers.Count > 0)
myUsers.RemoveAt (myUsers.Count - 1);
// this.listBox.ItemsSource = myUsers;
}
}
public class User
{
public User ( string name, string address)
{
Name = name;
Address = address;
}
public string Name { get; set; }
public string Address { get; set; }
}
}
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
namespace ListBoxDemo
{
public partial class MainPage : UserControl
{
public MainPage ()
{
InitializeComponent ();
BindingData ();
}
ObservableCollection<User> myUsers = new ObservableCollection<User> (); // 数据源
private void BindingData ()
{
myUsers.Clear ();
myUsers.Add ( new User ( " A1 ", " A11 "));
myUsers.Add ( new User ( " A2 ", " A12 "));
myUsers.Add ( new User ( " A3 ", " A13 "));
myUsers.Add ( new User ( " A4 ", " A14 "));
myUsers.Add ( new User ( " A5 ", " A15 "));
this.listBox.ItemsSource = myUsers; // 绑定数据源
}
private void buttonAdd_Click ( object sender, RoutedEventArgs e)
{
myUsers.Add ( new User ( " A " + (myUsers.Count + 1), " A1 " + myUsers.Count + 1));
// this.listBox.ItemsSource = myUsers;
}
private void buttonRemove_Click ( object sender, RoutedEventArgs e)
{
if (myUsers.Count > 0)
myUsers.RemoveAt (myUsers.Count - 1);
// this.listBox.ItemsSource = myUsers;
}
}
public class User
{
public User ( string name, string address)
{
Name = name;
Address = address;
}
public string Name { get; set; }
public string Address { get; set; }
}
}