16
2014
10

Silverlight中TreeView使用CheckBox实现多选功能

        <controls:TreeView x:Name="treeview_1" Height="Auto" Width="*" Margin="10" BorderThickness="0" HorizontalAlignment="Left"
                                    VerticalAlignment="Top"></controls:TreeView>

 

 

 

 

        private void Init_Treeview1()
        {
            TreeViewItem tvi_0 = new TreeViewItem();
            tvi_0.Name = "0";
            StackPanel sp = new StackPanel();
            CheckBox cb = new CheckBox();
            cb.Checked += new RoutedEventHandler((sender, e) =>
            {
                foreach (TreeViewItem item in tvi_0.Items)
                {
                    ((item.Header as StackPanel).Children[0] as CheckBox).IsChecked = true;
                }
            });
            cb.Unchecked += new RoutedEventHandler((sender, e) =>
            {

                foreach (TreeViewItem item in tvi_0.Items)
                {
                    ((item.Header as StackPanel).Children[0] as CheckBox).IsChecked = false;
                }
            });
            cb.Content = "所有";
            sp.Children.Add(cb);
            tvi_0.Header = sp;//关键就是在 这个 Header 
            tvi_0.IsExpanded = true;
            treeview_1.Items.Add(tvi_0);
            TreeViewItem tviTmp = null;
            StackPanel sptmp = null;
            CheckBox cbtmp = null;
            for (int i = 0; i < 100; i++)
            {
                tviTmp = new TreeViewItem();
                sptmp = new StackPanel();
                cbtmp = new CheckBox();

                tviTmp.Name = "tviTmp" + i.ToString();
                cbtmp.Name = "cbtmp" + i.ToString();
                sptmp.Name = "sptmp" + i.ToString();

                cbtmp.Content = "CheckBox" + i.ToString();
                sptmp.Children.Add(cbtmp);
                tviTmp.Header = sptmp;
                tvi_0.Items.Add(tviTmp);
            }
        }

 

« 上一篇下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。