项目要用到复选框,可是在Silverlight中不存在CheckBoxList。通过查阅资料以及依据自己的理解,写了简单演示样例:

1.XAML

<UserControl x:Class="SilverlightApplication1.CheckboxList"
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"
xmlns:local="clr-namespace:SilverlightApplication1"
xmlns:controlsToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<ListBox x:Name="lst">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<controlsToolkit:WrapPanel Orientation="Vertical" Height="30" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Grid>
</UserControl>

当中这里要引用Silverlight 3 Toolkit中的WrapPanel面板

xmlns:controlsToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"

2.CS

namespace SilverlightApplication1
{
public partial class CheckboxList : UserControl
{
#region 属性注冊
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(CheckboxList), new PropertyMetadata(null, ItemsSourceChanged)); public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.Register("SelectedItems", typeof(IEnumerable), typeof(CheckboxList), new PropertyMetadata(null)); public static readonly DependencyProperty DisplayMemberPathProperty =
DependencyProperty.Register("DisplayMemberPath", typeof(string), typeof(CheckboxList), new PropertyMetadata(string.Empty)); private static ObservableCollection<InternalModel> _internalCollection; /// <summary>
/// 数据源
/// </summary>
public IEnumerable ItemsSource
{
get { return GetValue(ItemsSourceProperty) as ObservableCollection<object>; }
set { SetValue(ItemsSourceProperty, value); }
} /// <summary>
/// 选择项
/// </summary>
public ObservableCollection<object> SelectedItems
{
get { return GetValue(SelectedItemsProperty) as ObservableCollection<object>; }
set { SetValue(SelectedItemsProperty, value); }
} /// <summary>
/// 显示字段
/// </summary>
public string DisplayMemberPath
{
get { return GetValue(DisplayMemberPathProperty) as string; }
set { SetValue(DisplayMemberPathProperty, value);
if (value != null)
lst.ItemTemplate =
(DataTemplate)XamlReader.Load(
@"<DataTemplate
xmlns=""http://schemas.microsoft.com/client/2007"">
<CheckBox Content=""{Binding Value." +
DisplayMemberPath +
@", Mode=TwoWay}"" IsChecked=""{Binding Selected, Mode=TwoWay}""/>
</DataTemplate>");
}
} private static void ItemsSourceChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
((CheckboxList)obj).BuildInternalCollection(e.NewValue as IEnumerable);
} private void BuildInternalCollection(IEnumerable collection)
{
_internalCollection = new ObservableCollection<InternalModel>();
foreach (var obj in collection)
{
var nContainerItem = new InternalModel { Selected = false, Value = obj };
nContainerItem.PropertyChanged += nContainerItem_PropertyChanged;
_internalCollection.Add(nContainerItem);
}
lst.ItemsSource = _internalCollection;
} void nContainerItem_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (SelectedItems == null)
SelectedItems = new ObservableCollection<object>(); SelectedItems.Clear(); foreach (var o in _internalCollection.Where(internalModel => internalModel.Selected))
SelectedItems.Add(o.Value);
}
#endregion
public CheckboxList()
{
InitializeComponent();
SelectedItems = new ObservableCollection<object>(); }
} public class InternalModel : INotifyPropertyChanged
{
public object Value { get; set; }
private bool _selected;
public bool Selected
{
get { return _selected; }
set
{
_selected = value;
NotifyPropertyChanged("Selected");
}
} public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}

主页面调用的方法

1.XAML

<UserControl x:Class="SilverlightApplication1.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"
xmlns:local="clr-namespace:SilverlightApplication1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<UserControl.Resources>
<local:People x:Key="folks"/>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="400"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
</Grid.RowDefinitions>
<local:CheckboxList x:Name="checkboxlist" ItemsSource="{Binding AllPeople,Source={StaticResource folks},Mode=TwoWay}" DisplayMemberPath="Name"/>
<Button Grid.Row="1" Content="显示选中项" Click="Button_Click" Width="60" HorizontalAlignment="Right" Margin="5"></Button>
</Grid>
</UserControl>

2.CS

namespace SilverlightApplication1
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
} private void Button_Click(object sender, RoutedEventArgs e)
{
string msg = string.Empty;
ObservableCollection<object> list = checkboxlist.SelectedItems;
foreach (var obj in list)
{
Person per = obj as Person; msg += per.Name + "\n";
}
MessageBox.Show(msg);
}
} public class Person
{
public int ID { get; set; }
public string Name { get; set; } } public class People
{
public People()
{
AllPeople = (from a in Enumerable.Range(1, 10)
select
new Person { ID = a, Name = "Name: " + a }
).ToList(); }
public List<Person> AllPeople { get; set; }
} }

源代码:Silverlight CheckBoxList

Silverlight CheckBoxList的更多相关文章

  1. Silverlight 后台设置 button 纯色背景

    silverlight Button直接设置其background为某一颜色往往达不到效果.因为其内置模板把按钮背景弄成一个渐变画刷.所以想要纯色的背景就修改其模板. 在后台修改模板的代码如下: St ...

  2. MVC CheckBoxList的实现

    using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; us ...

  3. 添加Silverlight应用到HTML

    Silverlight是跨浏览器,跨客户平台的浏览器插件,可以应用在Windows,Linux,Mac等平台.作为浏览器插件,Silverlight可以像Flash一样,很方便的嵌套在HTML页面中, ...

  4. Silverlight 手鼓达人-仿太鼓达人 开源

    Silverlight 手鼓达人-仿太鼓达人 介绍  手鼓达人是本人2012年中silverlight最火的一段时间开发的,本来目的只是想研究一下silverlight做游戏和做应用有何不同,但是后面 ...

  5. silverlight使用小计(先做记录后续整理)

    1.Grid: a.通过获取指定行的高度和指定列的宽度来获取指定单元格的宽高 b.几种宽高默认值: 宽高(Width/Heigth):1* 最大宽高(MaxWidth/MaxHeigth):正无穷大 ...

  6. SilverLight抛出 System.InvalidOperationException: 超出了2083 的最大URI

    在SilverLight中对于抛出 System.InvalidOperationException: 超出了 2083 的最大 URI 长度 的异常 处理 其实很简单 在 EntityFramewo ...

  7. 【Silverlight】打开Silverlight程序报错,"未找到导入的项目......请确认<Import>声明中的路径正确,且磁盘上存在该文件"

    在打开Silverlight程序时,报错(如图所示),程序使用的是Visual Studio 2013和最新的Silverlight版本(Silverlight5). 然后我在网上找了下说:Silve ...

  8. Silverlight 使用DataContractJsonSerializer序列化与反序列化 Json

    环境说明:Silverlight 5.1,.Net Framework  ​4.0 1.添加引用System.ServiceModel.Web.dll. 因为 System.Runtime.Seria ...

  9. Silverlight及WPF中实现自定义BusyIndicator

    在开发Silverlight或者WPF项目时,当我们调用Web服务来加载一些数据时,由于数据量比较大需要较长的时间,需要用户等待,为了给用户友好的提示和避免用户在加载数据过程中进行重复操作,我们通常使 ...

随机推荐

  1. 浅谈Qt事件的路由机制:鼠标事件

    请注意,本文是探讨文章而不是教程,是根据实验和分析得出的结果,可能是错的,因此欢迎别人来探讨和纠正. 这几天对于Qt的事件较为好奇,平时并不怎么常用,一般都是用信号,对于事件的处理,一般都是需要响应键 ...

  2. tengine install

    ./configure --prefix=/home/admin/local/tengine --with-http_stub_status_module --with-http_ssl_module ...

  3. 项目需要简单些了个WEB APP 的弹出窗

    var diag = { showDlg:'',//需要显示的DIV rushdiv:"<div id=v style='top:0;position: absolute;z-inde ...

  4. 对discuz的代码分析学习(二)首页文件

    如果当前地址栏存在查询字符,并且是一个数字,条件成立. 查询字符:www.baidu.com/index.php?aaa=bbb              aaa=bbb就是查询字符如果条件成立,则把 ...

  5. shell实现死循环

    参考自http://codingstandards.iteye.com/blog/780524 .while true do command; done .while : do command; do ...

  6. 《Pointers On C》读书笔记(第五章 操作符和表达式)

    1.C语言操作符优先级表 2.算术操作符中%(取模操作符)只适用于整型类型,其余几个操作符(+.-.*./)既适用于整型类型也适用于浮点类型.当/操作符的两个操作数都是整型时,它执行整除运算,其它情况 ...

  7. SQL Server 多表删除

    第一步: 建表 create table t1(x int, y int); create table t2(x int, y int); go insert into t1(x,y) values( ...

  8. 苹果iPhone不能判断红外发射管的好坏

    用手机来检测红外发射管好坏是目前比较常用的方法.实际操作比较简单,就是按照红外发射管的工作电压给发射管接上电源后,把手机的摄像头对着红外发射管就能看出好坏了.由于红外线是肉眼看不见的,如果不通过手机摄 ...

  9. Pro/Engineer wildfire 5.0 野火版系列下载及安装方法

    三.PTC Pro/Engineer wildfire 5.0 M030 野火版最新版 DVD 下载(多国语言) 1.野火下载站下载32&64位下载:[32位] http://down.pro ...

  10. 利用 XPath-jQuery 集锦手册在 XPath 和 jQuery 之间做选择

    利用 XPath-jQuery 集锦手册在 XPath 和 jQuery 之间做选择 利用 XPath-jQuery 集锦手册在 XPath 和 jQuery 之间做选择