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服务来加载一些数据时,由于数据量比较大需要较长的时间,需要用户等待,为了给用户友好的提示和避免用户在加载数据过程中进行重复操作,我们通常使 ...
随机推荐
- C#学习日志 day 2 ------ 控制台颜色以及windowsphone 窗体应用试建
成功跑起来了hello之后,试试改变背景颜色.
- Linux系统管理员:不要害怕升级内核
Linux系统管理员平时很重要的一项工作就是负责系统内核升级.做好系统内核的升级工作,对于Linux系 统的稳定性具有至关重要的作用.但是很少有人敢贸然的对Linux系统的内核进行升级,担心会影响现有 ...
- table常用
<style> table,table td { border: 1px solid #ccc; border-collapse:collapse; } </style> 注意 ...
- BASE64URL解析
BASE64URL是一种在BASE64的基础上编码形成新的加密方式,为了编码能在网络中安全顺畅传输,需要对BASE64进行的编码,特别是互联网中. BASE64URL编码的流程: .明文使用BASE6 ...
- Centos系统mysql 忘记root用户的密码:
第一步:(停掉正在运行的mysql) [root@maomao ~]# service mysqld stop Stopping MySQL: ...
- [js - 算法可视化] 汉诺塔(Hanoi)演示程序
前段时间偶然看到有个日本人很早之前写了js的多种排序程序,使用js+html实现的排序动画,效果非常好. 受此启发,我决定写几个js的算法动画,第一个就用汉诺塔. 演示地址:http://tut.ap ...
- linux系统巡检脚本shell实例
#!/bin/sh BACKUP_TIMESTAMP=`date +%Y%m%d` HOSTNAME=`hostname` num=89 ###################核查文件系统opt### ...
- 【hadoop】14、hadoop2.5的mapreduce的 配置
配置mapreduce <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href ...
- linux之多进程fork:进程通信
++++++++++++++++++信号机制+++++++++++++++++++ 接收信号 int signal(int sig,__sighandler_t handler); int func( ...
- GridView行编辑、更新、取消、删除事件使用方法
注意:当启用编辑button时,点击编辑button后会使一整行都切换成文本框.为了是一行中的一部分是文本框,须要把以整行的全部列都转换成模板,然后删掉编辑模板中的代码.这样就能使你想编辑的列转换成文 ...