WPF自定义CheckedListBox控件----写给自己

来源:互联网 发布:碎片整理软件 编辑:程序博客网 时间:2024/05/06 20:57

这篇留给自己的笔记。

首先,需要的功能如下图:

 

在winform中有控件CheckedListBox,WPF 中没有,只能自定义,网上有很多这类代码,由于新手的原因,很多拿到手代码不止从何开始更改...............


界面设计代码,如下图:

XAML:

 <Grid>        <ListBox Name="listbox" SelectionMode="Multiple" Width="106" Height="100" Margin="132,72,265,139">            <!--<ListBox.Template>                <ControlTemplate>                    <CheckBox Focusable="False" Width="100" Content="{Binding chkName}" IsChecked="{Binding chkIsCheck}"/>                </ControlTemplate>            </ListBox.Template>-->                <ListBox.ItemTemplate>                    <DataTemplate>                        <CheckBox Focusable="False" Width="100" Content="{Binding chkName}" IsChecked="{Binding chkIsCheck}" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" />                    </DataTemplate>                </ListBox.ItemTemplate>        </ListBox>        <Button Content="loadata" Click="btnload_Click" Name="btnload" Margin="12,169,0,117" Width="100" Height="25" HorizontalAlignment="Left" VerticalAlignment="Center"/>        <Button Content="clear" Height="25" HorizontalAlignment="Left" Margin="12,217,0,0" Name="button1" VerticalAlignment="Top" Width="100" Click="button1_Click" />        <ListBox Height="156" HorizontalAlignment="Left" Margin="297,38,0,0" Name="listBox1" VerticalAlignment="Top" Width="120" />        <CheckBox Content="Check all" Height="19" HorizontalAlignment="Left" Margin="132,178,0,0" Name="checkBox1" VerticalAlignment="Top" Click="checkBox1_Click" />        <CheckBox Content="Uncheck all" Height="19" HorizontalAlignment="Left" Margin="132,201,0,0" Name="checkBox2" VerticalAlignment="Top" Click="checkBox2_Click" />    </Grid>


后台代码:

 int i = 9;        private void btnload_Click(object sender, RoutedEventArgs e)        {            i++;            //添加单条数据            BindingList<ListBoxModel> list = listbox.ItemsSource as BindingList<ListBoxModel>;            list.Insert(0, new ListBoxModel() {             chkIsCheck=false,            chkName=i+""+i            });            listbox.ItemsSource = new BindingList<ListBoxModel>(list);                  }        private void Window_Loaded(object sender, RoutedEventArgs e)        {//加载初始数据            List<ListBoxModel> list = new List<ListBoxModel>();            list.Add(new ListBoxModel()            {                chkName = "1",                chkIsCheck = false            });            list.Add(new ListBoxModel()            {                chkName = "2",                chkIsCheck = false            });            listbox.ItemsSource = new BindingList<ListBoxModel>(list);        }        private void CheckBox_Checked(object sender, RoutedEventArgs e)        {                        CheckBox chk = sender as CheckBox;            if (chk != null)            {                if (!listBox1.Items.Contains(chk.Content))                    listBox1.Items.Add(chk.Content.ToString());            }        }        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)        {            CheckBox chk = sender as CheckBox;            if (chk != null)            {                listBox1.Items.Remove(chk.Content.ToString());            }        }        private void checkBox1_Click(object sender, RoutedEventArgs e)        {//全选            checkBox2.IsChecked = false;            BindingList<ListBoxModel> list = (BindingList<ListBoxModel>)listbox.ItemsSource;// as BindingList<ListBoxModel>;            if (list.Count > 0)            {                if (checkBox1.IsChecked == true)                {                    for (int i = 0; i < list.Count; i++)                    {                        if (!list[i].chkIsCheck)                        {                            list[i].chkIsCheck = true;                            if (!listBox1.Items.Contains(list[i].chkName))                                listBox1.Items.Add(list[i].chkName);                        }                    }                }                else                {                    listBox1.Items.Clear();                    for (int i = 0; i < list.Count; i++)                    {                        list[i].chkIsCheck = false;                    }                 }                listbox.ItemsSource = new BindingList<ListBoxModel>(list);            }        }        private void checkBox2_Click(object sender, RoutedEventArgs e)        {//反选            checkBox1.IsChecked = false;            BindingList<ListBoxModel> list = (BindingList<ListBoxModel>)listbox.ItemsSource;// as BindingList<ListBoxModel>;            for (int i = 0; i < list.Count; i++)            {                if (!list[i].chkIsCheck)                {                    list[i].chkIsCheck = true;                    if (!listBox1.Items.Contains(list[i].chkName))                        listBox1.Items.Add(list[i].chkName);                }                else                {                    list[i].chkIsCheck = false;                    listBox1.Items.Remove(list[i].chkName);                }            }            listbox.ItemsSource = new BindingList<ListBoxModel>(list);        } private void button1_Click(object sender, RoutedEventArgs e)        {//初始添加数据            BindingList<ListBoxModel> list = listbox.ItemsSource as BindingList<ListBoxModel>;            list[1].chkIsCheck = true;            listbox.ItemsSource = new BindingList<ListBoxModel>(list);        }

ListBoxModel:
<pre name="code" class="csharp">public class ListBoxModel    {        public string chkName { get; set; }        public bool chkIsCheck { get; set; }    }







以上都完成了,就可以实现第一张动态图的功能了。。

当然这种方式的CheckedListBox有个bug,CheckBox的宽度需算的很准跟ListBox的宽度。

WPF自定义CheckedListBox源码


0 0
原创粉丝点击