Silverlight CheckBoxList, silverlight

Source: Internet
Author: User

Silverlight CheckBoxList, silverlight

Check boxes are used for the project, but there is no CheckBoxList in Silverlight. A simple example is provided based on your understanding and materials:

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>

Here we will reference the WrapPanel panel in the Silverlight 3 Toolkit.

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

Namespace SilverlightApplication1 {public partial class CheckboxList: UserControl {# region property registration public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty. register ("ItemsSource", typeof (IEnumerable), typeof (CheckboxList), new PropertyMetadata (null, ItemsSourceChanged); public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty. register ("SelectedI Tems ", 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> // data source // </summary> public IEnumerable ItemsSource {get {return GetValue (ItemsSourceProperty) as ObservableCollection <object >;} set {SetValue (ItemsSourceProperty, value );}} /// <summary> /// select items /// </summary> public ObservableCollection <object> SelectedItems {get {return GetValue (SelectedItemsProperty) as ObservableCollection <object> ;} set {SetValue (SelectedItemsProperty, value) ;}/// <summary> // display field /// </summary> pu Blic 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 ). build InternalCollection (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. it EmsSource = _ 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 (); Selected Items = new ObservableCollection <object> () ;}} public class InternalModel: INotifyPropertyChanged {public object Value {get; set;} private bool _ selected; public bool Selected {get {return _ selected;} set {_ selected = value; yypropertychanged ("Selected") ;}} public event PropertyChangedEventHandler PropertyChanged; public void policypropertychanged (string propertyName) {if (Property Changed! = Null) {PropertyChanged (this, new PropertyChangedEventArgs (propertyName ));}}}}

Homepage call Method

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 = "show selected items" 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; }    }     }


Source code: Silverlight CheckBoxList

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.