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服务来加载一些数据时,由于数据量比较大需要较长的时间,需要用户等待,为了给用户友好的提示和避免用户在加载数据过程中进行重复操作,我们通常使 ...
随机推荐
- TCP的流量控制
TCP协议作为一个可靠的面向字节流的传输协议,其可靠性和流量控制由滑动窗口协议保证,而拥塞控制则由控制窗口结合一系列的控制算法实现. 要区分TCP的流量控制和拥塞控制: 流量控制是发送方的发送数据的速 ...
- C++中的unordered_map
1.简介 随着C++0x标准的确立,C++的标准库中也终于有了hash table这个东西.很久以来,STL中都只提供<map>作为存放对应关系的容器,内部通常用红黑树实现,据说原因是二叉 ...
- 5G关键技术研究方向
对于还没体验4G移动通信魅力的国内的移动通信用户而言,5G也许还是镜中花,雾中月:但对于科研界而言,5G研究已经启程,三星电子5月份宣布,率先开发出了首个基于5G核心技术的移动传输网络,实现每秒1Gb ...
- 为IE6-7间接支持:before和:after伪类
:before和:after我们经常会用到,特别是在做移动端页面时,利用它制作文字前后的ICON.图片的垂直居中之类的非常方便且代码简洁(当然,功能远比这些要多的多...). 可是在PC端,由于现在还 ...
- Linux cd命令
1: cd 不加任何参数,它会自动跳到用户的家目录中去! 2: ~ 表示用户的家目录 3: cd ~userNmae/ 这样可以进入指定用户的家目录中去! 4: cd - 跳到上一次所在的目 ...
- 分享一个MD5加密工具类
来自:http://blog.csdn.net/zranye/article/details/8234480 Es:http://blog.csdn.net/longxibendi/article/d ...
- hdu 4612 Warm up 双连通缩点+树的直径
首先双连通缩点建立新图(顺带求原图的总的桥数,事实上因为原图是一个强连通图,所以桥就等于缩点后的边) 此时得到的图类似树结构,对于新图求一次直径,也就是最长链. 我们新建的边就一定是连接这条最长链的首 ...
- DataSet - DataTable - DataRow 读取 xml 文件 + 搜索
DataSet ds = XmlHelper.GetDataSetByXml(AppDomain.CurrentDomain.BaseDirectory + "/Config/ConfigN ...
- Node.js笔记4
4. 文件系统 fs fs模块是文件操作的封装,提供了同步跟异步操作2个版本 * fs.readFile(filename,[encoding],[callback(err,data)]) 是最简单的 ...
- ztree树形插件
在开发项目中需要用到树插件,近期研究了几款树插件,好记性不如烂笔头 ,写下来 以后好查 MzTreeView(梅花雪) 很经典的树形菜单脚本控件 菜单树展示加载速度快 支持1w条以上大数据 缺点-- ...