热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> Silverlight>>正文内容

Silverlight实现可换行换列的CheckBoxList

发布时间:2010年03月23日点击数: 佚名

在Silverlight中实现CheckBoxList一般都使用ListBox+CheckBox实现
但如何实现类似Asp.net控件CheckBoxList的RepeatColumns功能呢?
如下图所示:

主要用到了ListBox的ItemsPanel,其中放入了一个ControlsToolkit的WrapPanel
一些国外的论坛里都提到用ListBox的父类控件ItemsControl
其实大可不必,ListBox本身就拥有ItemsPanel,在派生时还加入了ScrollViewer,也就是自带滚动条
而ItemsControl是没有ScrollViewer的,需要用一个ScrollViewer包住,何必多此一举呢?

XAML代码

  1. <UserControl 
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4.     xmlns:controlsToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"  
  5.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  6.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  7.     x:Class="SilverlightSample.UcTest2" 
  8.     mc:Ignorable="d" > 
  9.     <Grid x:Name="LayoutRoot" Background="White" Height="272" Width="392"> 
  10.         <Grid Height="150" Margin="20,22,20,100"> 
  11.                 <ListBox  x:Name="checkBoxList1"> 
  12.                     <ListBox.ItemsPanel> 
  13.                         <ItemsPanelTemplate> 
  14.                             <!--WrapPanel的Height除以CheckBox的Height决定每列的行数--> 
  15.                             <!--暂未找到可以设置WrapPanel折行数量的属性--> 
  16.                             <!--Orientation设置纵向或横向排列,横向的话需要用Width设置--> 
  17.                             <controlsToolkit:WrapPanel Orientation="Vertical" Height="100" /> 
  18.                         </ItemsPanelTemplate> 
  19.                     </ListBox.ItemsPanel> 
  20.                     <ListBox.ItemTemplate> 
  21.                         <DataTemplate> 
  22.                             <!--这里的Selected属性设置双向绑定是为了筛选选中的项目--> 
  23.                             <CheckBox IsChecked="{Binding Selected, Mode=TwoWay}" Height="16" > 
  24.                                 <TextBlock Text="{Binding Name}" FontSize="13"  /> 
  25.                             </CheckBox> 
  26.                         </DataTemplate> 
  27.                     </ListBox.ItemTemplate> 
  28.                 </ListBox> 
  29.         </Grid> 
  30.         <Button Margin="179,199,94,41" Content="获取选中" FontSize="13"  Click="Button_Click"/> 
  31.     </Grid> 
  32. </UserControl> 

C#代码

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Net; 
  5. using System.Windows; 
  6. using System.Windows.Controls; 
  7. using System.Windows.Documents; 
  8. using System.Windows.Input; 
  9. using System.Windows.Media; 
  10. using System.Windows.Media.Animation; 
  11. using System.Windows.Shapes; 
  12.  
  13. using SilverlightSample.ServiceReference; 
  14.  
  15. namespace SilverlightSample 
  16.     public partial class UcTest2 : UserControl 
  17.     { 
  18.         public UcTest2() 
  19.         { 
  20.             InitializeComponent(); 
  21.  
  22.             WebServiceSoapClient sc = new WebServiceSoapClient(); 
  23.  
  24.             sc.getCategoryListCompleted += (s, e) => 
  25.             { 
  26.                 if (e.Error == null
  27.                 { 
  28.                     checkBoxList1.ItemsSource = e.Result; 
  29.                 } 
  30.             }; 
  31.  
  32.             sc.getCategoryListAsync(); 
  33.         } 
  34.  
  35.         private void Button_Click(object sender, RoutedEventArgs e) 
  36.         { 
  37.             //获取选中的项目 
  38.             IEnumerable<Category> list = (IEnumerable<Category>)checkBoxList1.ItemsSource; 
  39.             IEnumerable<Category> selectedList=list.Where(a=>a.Selected==true); 
  40.         } 
  41.     } 

WebService代码

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Web; 
  4. using System.Web.Services; 
  5. /// <summary> 
  6. ///WebService 的摘要说明 
  7. /// </summary> 
  8. [WebService(Namespace = "http://www.shanghaimart.com/")] 
  9. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
  10. public class WebService : System.Web.Services.WebService 
  11.     public WebService() 
  12.     { 
  13.  
  14.         //如果使用设计的组件,请取消注释以下行  
  15.         //InitializeComponent();  
  16.     } 
  17.     [WebMethod] 
  18.     public List<Category> getCategoryList() 
  19.     { 
  20.         List<Category> list = new List<Category>(); 
  21.         list.Add(new Category(1, "搭建管理费"true)); 
  22.         list.Add(new Category(2, "电费"false)); 
  23.         list.Add(new Category(3, "广告阵地费"true)); 
  24.         list.Add(new Category(4, "加班费"false)); 
  25.         list.Add(new Category(5, "物品损坏赔偿"true)); 
  26.         list.Add(new Category(6, "空调费"false)); 
  27.         list.Add(new Category(7, "展会折扣"true)); 
  28.         list.Add(new Category(8, "租用物品"false)); 
  29.         list.Add(new Category(9, "展厅增租"true)); 
  30.         list.Add(new Category(10, "开幕式服务"false)); 
  31.         list.Add(new Category(11, "租金"true)); 
  32.         list.Add(new Category(12, "押金"false)); 
  33.         list.Add(new Category(13, "服务费"true)); 
  34.         list.Add(new Category(14, "收款"false)); 
  35.         list.Add(new Category(15, "保证金"true)); 
  36.         list.Add(new Category(16, "定金"false)); 
  37.         list.Add(new Category(17, "税费"false)); 
  38.         return list; 
  39.     } 
  40. [Serializable] 
  41. public class Category 
  42.     public Category() 
  43.     { } 
  44.     public Category(int id, string name, bool selected) 
  45.     { 
  46.         ID = id; 
  47.         Name = name; 
  48.         Selected = selected; 
  49.     } 
  50.     public int ID; 
  51.     public string Name; 
  52.     public bool Selected; 

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作