ListBox实际上跟ComboBox差不多,一个是下拉列表,一个是全拉开了的列表 - - 而两个控件的常用属性也都差不多,所以ComboBox中讲到的这篇就不再重复说了,今天主要说说两个ListBox的联动实现
MainPage.xaml
页面有三个ListBox,listbox为加载时的填入的数据,listbox2是接收listbox的SelectionChanged方法传入的数据,listbox3是接收listbox通过按钮adds批量传入的数据
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<
Grid x:Name
=
"
LayoutRoot
"
Loaded
=
"
LayoutRoot_Loaded
"
HorizontalAlignment
=
"
Left
"
>
< Grid.RowDefinitions >
< RowDefinition ></ RowDefinition >
</ Grid.RowDefinitions >
< Grid.ColumnDefinitions >
< ColumnDefinition ></ ColumnDefinition >
</ Grid.ColumnDefinitions >
< StackPanel Orientation = " Horizontal " Grid.Column = " 0 " Grid.Row = " 0 " Margin = " 0,10,0,0 " >
< StackPanel Orientation = " Vertical " >
< StackPanel Orientation = " Horizontal " >
< ListBox x:Name = " listbox " Width = " 128 " Height = " 150 " SelectionMode = " Extended " SelectionChanged = " listbox_SelectionChanged " >
<!--
SelectionMode属性
Extended 支持 Ctrl Shift键
Multiple 默认按下Ctrl键 不支持Shift键
Single 支持 Ctrl键 不支持Shift键
-->
</ ListBox >
< ListBox x:Name = " listbox2 " Width = " 128 " Height = " 150 " ></ ListBox >
< ListBox x:Name = " listbox3 " Width = " 128 " Height = " 150 " ></ ListBox >
</ StackPanel >
< TextBlock x:Name = " List_name " Width = " 300 " Height = " 24 " Text = " 开始 " >
</ TextBlock >
<!-- < Button x:Name = " bools " Content = " 禁用ListBox1的SelectionChanged事件 " Height = " 24 " Width = " 230 " Click = " bools_Click " ></ Button > -->
< Button x:Name = " adds " Content = " LB1批量加到LB3中 " Height = " 24 " Width = " 230 " Click = " adds_Click " ></ Button >
</ StackPanel >
</ StackPanel >
</ Grid >
< Grid.RowDefinitions >
< RowDefinition ></ RowDefinition >
</ Grid.RowDefinitions >
< Grid.ColumnDefinitions >
< ColumnDefinition ></ ColumnDefinition >
</ Grid.ColumnDefinitions >
< StackPanel Orientation = " Horizontal " Grid.Column = " 0 " Grid.Row = " 0 " Margin = " 0,10,0,0 " >
< StackPanel Orientation = " Vertical " >
< StackPanel Orientation = " Horizontal " >
< ListBox x:Name = " listbox " Width = " 128 " Height = " 150 " SelectionMode = " Extended " SelectionChanged = " listbox_SelectionChanged " >
<!--
SelectionMode属性
Extended 支持 Ctrl Shift键
Multiple 默认按下Ctrl键 不支持Shift键
Single 支持 Ctrl键 不支持Shift键
-->
</ ListBox >
< ListBox x:Name = " listbox2 " Width = " 128 " Height = " 150 " ></ ListBox >
< ListBox x:Name = " listbox3 " Width = " 128 " Height = " 150 " ></ ListBox >
</ StackPanel >
< TextBlock x:Name = " List_name " Width = " 300 " Height = " 24 " Text = " 开始 " >
</ TextBlock >
<!-- < Button x:Name = " bools " Content = " 禁用ListBox1的SelectionChanged事件 " Height = " 24 " Width = " 230 " Click = " bools_Click " ></ Button > -->
< Button x:Name = " adds " Content = " LB1批量加到LB3中 " Height = " 24 " Width = " 230 " Click = " adds_Click " ></ Button >
</ StackPanel >
</ StackPanel >
</ Grid >
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#region
页面加载
private void LayoutRoot_Loaded( object sender, RoutedEventArgs e)
{
Load_ListBox();
}
#endregion
#region 加载ComboBox数据
public void Load_ListBox()
{
List < Model.Person > list = new List < Model.Person > ();
for ( int i = 0 ; i < 30 ; i ++ ) // 写一个循环用来增加数据
{
Model.Person ps = new Model.Person(); // Person实体类
ps.ID = i;
ps.Name = " Terry " + i;
ps.Age = " abc " + i;
list.Add(ps);
// list.Sort(new Tool.Comparer()); // list的排序方法,暂时不说
}
if (list.Count > 0 )
{
listbox.ItemsSource = list; // 指定数据源
listbox.DisplayMemberPath = " Name " ; // 指定ListBox显示的字段
}
}
#endregion
#region 选择某一项时触发ListBox的事件(选择左边的listbox将数据添加到右边的listbox中)
List < Model.Person > list1 = new List < Model.Person > ();
private void listbox_SelectionChanged( object sender, SelectionChangedEventArgs e)
{
// 适用于单选
Model.Person p = (Model.Person)listbox.SelectedItem; // 获得选中的对象
if (listbox2.Items.Count > 0 ) // 判断list是否为空
{
if ( ! list1.Contains < Model.Person > (p)) // 判断要添加到list中的对象是否存在于list
{
list1.Add(p); // 如果不存在则添加到list中
// list1.Sort(new Tool.Comparer()); // list的排序方法,暂时不说
}
}
else
{
list1.Add(p); // list为空则加到list中
// list1.Sort(new Tool.Comparer()); // list的排序方法,暂时不说
}
listbox2.ItemsSource = null ; // 将listbox数据源清空 必须这样做 否则listbox无法加载新的list
listbox2.ItemsSource = list1;
listbox2.DisplayMemberPath = " Name " ;
List_name.Text = " 当前选择的是: " + p.Name.ToString();
}
#endregion
#region 把ListBox1的内容批量添加到ListBox3中
List < Model.Person > _list = new List < Model.Person > ();
private void adds_Click( object sender, RoutedEventArgs e)
{
if (_list.Count > 0 ) // 如果list不为空说明已传入过数据
{
for ( int i = 0 ; i < listbox.SelectedItems.Count; i ++ ) // 循环listbox选中的数据
{
Model.Person p = (Model.Person)listbox.SelectedItems[i]; // 将选中的依次强转为Model.Person类型(实体类)
if ( ! _list.Contains < Model.Person > (p)) // 检索list中是否存在该对象,如果不存在则添加到list中,去重复的方法
{
_list.Add(p);
// _list.Sort(new Tool.Comparer()); // list的排序方法,暂时不说
}
}
}
else
{
for ( int j = 0 ; j < listbox.SelectedItems.Count; j ++ ) // 循环listbox选中的数据
{
_list.Add(((Model.Person)listbox.SelectedItems[j]));
// _list.Sort(new Tool.Comparer()); // list的排序方法,暂时不说
}
}
listbox3.ItemsSource = null ;
listbox3.ItemsSource = _list;
listbox3.DisplayMemberPath = " Name " ;
}
#endregion
private void LayoutRoot_Loaded( object sender, RoutedEventArgs e)
{
Load_ListBox();
}
#endregion
#region 加载ComboBox数据
public void Load_ListBox()
{
List < Model.Person > list = new List < Model.Person > ();
for ( int i = 0 ; i < 30 ; i ++ ) // 写一个循环用来增加数据
{
Model.Person ps = new Model.Person(); // Person实体类
ps.ID = i;
ps.Name = " Terry " + i;
ps.Age = " abc " + i;
list.Add(ps);
// list.Sort(new Tool.Comparer()); // list的排序方法,暂时不说
}
if (list.Count > 0 )
{
listbox.ItemsSource = list; // 指定数据源
listbox.DisplayMemberPath = " Name " ; // 指定ListBox显示的字段
}
}
#endregion
#region 选择某一项时触发ListBox的事件(选择左边的listbox将数据添加到右边的listbox中)
List < Model.Person > list1 = new List < Model.Person > ();
private void listbox_SelectionChanged( object sender, SelectionChangedEventArgs e)
{
// 适用于单选
Model.Person p = (Model.Person)listbox.SelectedItem; // 获得选中的对象
if (listbox2.Items.Count > 0 ) // 判断list是否为空
{
if ( ! list1.Contains < Model.Person > (p)) // 判断要添加到list中的对象是否存在于list
{
list1.Add(p); // 如果不存在则添加到list中
// list1.Sort(new Tool.Comparer()); // list的排序方法,暂时不说
}
}
else
{
list1.Add(p); // list为空则加到list中
// list1.Sort(new Tool.Comparer()); // list的排序方法,暂时不说
}
listbox2.ItemsSource = null ; // 将listbox数据源清空 必须这样做 否则listbox无法加载新的list
listbox2.ItemsSource = list1;
listbox2.DisplayMemberPath = " Name " ;
List_name.Text = " 当前选择的是: " + p.Name.ToString();
}
#endregion
#region 把ListBox1的内容批量添加到ListBox3中
List < Model.Person > _list = new List < Model.Person > ();
private void adds_Click( object sender, RoutedEventArgs e)
{
if (_list.Count > 0 ) // 如果list不为空说明已传入过数据
{
for ( int i = 0 ; i < listbox.SelectedItems.Count; i ++ ) // 循环listbox选中的数据
{
Model.Person p = (Model.Person)listbox.SelectedItems[i]; // 将选中的依次强转为Model.Person类型(实体类)
if ( ! _list.Contains < Model.Person > (p)) // 检索list中是否存在该对象,如果不存在则添加到list中,去重复的方法
{
_list.Add(p);
// _list.Sort(new Tool.Comparer()); // list的排序方法,暂时不说
}
}
}
else
{
for ( int j = 0 ; j < listbox.SelectedItems.Count; j ++ ) // 循环listbox选中的数据
{
_list.Add(((Model.Person)listbox.SelectedItems[j]));
// _list.Sort(new Tool.Comparer()); // list的排序方法,暂时不说
}
}
listbox3.ItemsSource = null ;
listbox3.ItemsSource = _list;
listbox3.DisplayMemberPath = " Name " ;
}
#endregion
Model.cs
跟上一个Silverlight4.0(4) 之 ComboBox 的例子一样建立一个类库
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public
class
Person
{
public int ID
{
get ;
set ;
}
public string Name
{
get ;
set ;
}
public string Age
{
get ;
set ;
}
}
{
public int ID
{
get ;
set ;
}
public string Name
{
get ;
set ;
}
public string Age
{
get ;
set ;
}
}
好了,这个例子实际上跟ComboBox中的例子差不多,只不过扩展了一下多ListBox的联动,做了一些List泛型的简单操作,我觉得与List相关的一些知识应该多看一些,因为目前看来,Silverlight中有关加载数据源的地方,列表容器用的还是比较多的。