WPF中的checkBoxList [英] checkBoxList in Wpf

查看:144
本文介绍了WPF中的checkBoxList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要Wpf中的复选框
使用xaml我在设计中得到了核对清单,但是我不知道如何在代码中使用它,这是我的xaml代码

i want checkboxlist in Wpf
using xaml i got the checkboxlist in design but i don''t know how to use this in code this is my xaml code

<ListBox x:Name="listExtraSkills" ItemsSource="{Binding CheckList}" Grid.Column="1" Grid.Row="6" Height="50" >
           <ListBox.ItemTemplate>
               <DataTemplate>
                   <CheckBox Name="chkitems" Content="{Binding TheText}" IsChecked="{Binding IsSelected }"  > </CheckBox>
               </DataTemplate>
           </ListBox.ItemTemplate>
       </ListBox>

推荐答案

此处的示例代码http://www.c-sharpcorner.com/uploadfile/syedshakeer/checkboxlist-in-wpf/ [ http://social.msdn.microsoft.com/论坛/eu/wpf/thread/a355a085-2650-45b6-a75b-b6a298e3f20d [
Sample code here http://www.c-sharpcorner.com/uploadfile/syedshakeer/checkboxlist-in-wpf/[^]

and here http://social.msdn.microsoft.com/Forums/eu/wpf/thread/a355a085-2650-45b6-a75b-b6a298e3f20d[^]


这里是一个基于您的XAML的工作示例:

首先,隐藏代码:
Here''s a working example, based on your XAML:

First of all, the code-behind:
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Documents;

namespace WpfApplicationCheckBoxList
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window, INotifyPropertyChanged
    {
        #region Instance Variables
        List<checklistitem> checkList;
        #endregion /Instance Variables

        #region INotifyPropertyChanged
        //Add this bit of code to all your code behinds
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, e);
            }
        }
        #endregion /INotifyPropertyChanged

        //Constructor
        public Window1()
        {
            InitializeComponent();
            CheckList = GenerateTestData();
        }

        //Property to bind to
        public List<checklistitem> CheckList
        {
            get { return checkList; }
            set { checkList = value; this.OnPropertyChanged(new PropertyChangedEventArgs("CheckList")); }
        }

        //Test data helper
        List<checklistitem> GenerateTestData()
        {
            List<checklistitem> checkListItems = new List<checklistitem>();
            checkListItems.Add(new CheckListItem { TheText = "My Text Here 1", IsSelected = false });
            checkListItems.Add(new CheckListItem { TheText = "My Text Here 2", IsSelected = true });
            checkListItems.Add(new CheckListItem { TheText = "My Text Here 3", IsSelected = false });
            return checkListItems;
        }
    }
}



记住要在XAML中设置DataContext:



Remember to set the DataContext in your XAML:

<window x:class="WpfApplicationCheckBoxList.Window1" xmlns:x="#unknown">
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" 
    Height="400" 
    Width="600"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <grid>
        <listbox x:name="listExtraSkills" itemssource="{Binding CheckList}" grid.column="1" grid.row="6" height="50">
            <listbox.itemtemplate>
                <datatemplate>
                    <checkbox name="chkitems" content="{Binding TheText}" ischecked="{Binding IsSelected }"></checkbox>
                </datatemplate>
            </listbox.itemtemplate>
        </listbox>
    </grid>
</window>



最后是您要绑定到列表的数据对象:



And finally the data object you''re binding to the list:

public class CheckListItem
{
    public string TheText { get; set; }
    public bool IsSelected { get; set; }
}


这篇关于WPF中的checkBoxList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆