Silverlight CheckBoxList
项目要用到复选框,可是在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 后台设置 button 纯色背景
silverlight Button直接设置其background为某一颜色往往达不到效果.因为其内置模板把按钮背景弄成一个渐变画刷.所以想要纯色的背景就修改其模板. 在后台修改模板的代码如下: St ...
- MVC CheckBoxList的实现
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; us ...
- 添加Silverlight应用到HTML
Silverlight是跨浏览器,跨客户平台的浏览器插件,可以应用在Windows,Linux,Mac等平台.作为浏览器插件,Silverlight可以像Flash一样,很方便的嵌套在HTML页面中, ...
- Silverlight 手鼓达人-仿太鼓达人 开源
Silverlight 手鼓达人-仿太鼓达人 介绍 手鼓达人是本人2012年中silverlight最火的一段时间开发的,本来目的只是想研究一下silverlight做游戏和做应用有何不同,但是后面 ...
- silverlight使用小计(先做记录后续整理)
1.Grid: a.通过获取指定行的高度和指定列的宽度来获取指定单元格的宽高 b.几种宽高默认值: 宽高(Width/Heigth):1* 最大宽高(MaxWidth/MaxHeigth):正无穷大 ...
- SilverLight抛出 System.InvalidOperationException: 超出了2083 的最大URI
在SilverLight中对于抛出 System.InvalidOperationException: 超出了 2083 的最大 URI 长度 的异常 处理 其实很简单 在 EntityFramewo ...
- 【Silverlight】打开Silverlight程序报错,"未找到导入的项目......请确认<Import>声明中的路径正确,且磁盘上存在该文件"
在打开Silverlight程序时,报错(如图所示),程序使用的是Visual Studio 2013和最新的Silverlight版本(Silverlight5). 然后我在网上找了下说:Silve ...
- Silverlight 使用DataContractJsonSerializer序列化与反序列化 Json
环境说明:Silverlight 5.1,.Net Framework 4.0 1.添加引用System.ServiceModel.Web.dll. 因为 System.Runtime.Seria ...
- Silverlight及WPF中实现自定义BusyIndicator
在开发Silverlight或者WPF项目时,当我们调用Web服务来加载一些数据时,由于数据量比较大需要较长的时间,需要用户等待,为了给用户友好的提示和避免用户在加载数据过程中进行重复操作,我们通常使 ...
随机推荐
- xhprof
#官网下载 http://pecl.php.net/package/xhprof tar zxf xhprof-0.9.2.tgz cd xhprof-0.9.2/extension/ sud ...
- Linux学习之输入输出重定向
转自:http://www.cnblogs.com/chengmo/archive/2010/10/20/1855805.html 多谢分享 在了解重定向之前,我们先来看看linux 的文件描述符. ...
- [LeetCode]题解(python):137-Single Number II
题目来源: https://leetcode.com/problems/single-number-ii/ 题意分析: 给定一个数组,数组里面每一个数字都出现了3次除了一个,找出那个数.要求时间复杂度 ...
- WPF:在XmlDataProvider上使用主-从绑定(Master-Detail Binding)
原文 http://www.cnblogs.com/mgen/archive/2011/06/19/2084553.html 示例程序: 如上程序截图,一目了然典型的主从模式绑定应用,如果里面的数据不 ...
- easyui-layout中的收缩层无法显示标题问题解决
先看问题描述效果图片: 如上,我的查询条件是放在layout下面的一个可收缩层中,初始是收缩的,title显示不出来的话对使用者很不方便,代码如下: <div id="__MODULE ...
- 深度优先搜索算法(DFS)以及leetCode的subsets II
深度优先搜索算法(depth first search),是一个典型的图论算法.所遵循的搜索策略是尽可能“深”地去搜索一个图. 算法思想是: 对于新发现的顶点v,如果它有以点v为起点的未探测的边,则沿 ...
- tp中phpexcel导出实例
public function phpexcel(){ //测试$this->display("User:xx");//跨模块分配页面User模块xx.html // xx\ ...
- InnoDB引擎Myslq数据库数据恢复
首先祝愿看到这片文章的你永远不要有机会用到它... 本文指针对用InnoDB引擎的Mysql数据库的数据恢复,如果是其它引擎的Mysql或其它数据库请自行google... 如果有一天你手挫不小心删掉 ...
- HEVC与VP9编码效率对比
HEVC(High EfficiencyVideo Coding,高效率视频编码)是一种视频压缩标准,H.264/MPEG-4 AVC的继任者.目前正在由ISO/IEC MPEG和ITU-T VCEG ...
- JavaScript 深入学习及常用工具方法整理 ---- 01.浮点数
在JavaScript中是不区分整数值和浮点数值的,其中所有的数字均用浮点数值表示.JavaScript采用IEEE 754标准(有兴趣可以浏览网络规范分类下的IEEE 754标准,需要原文件请在留言 ...