treeview checkbox選中的時候選擇子節點 mvvm

來源:互聯網
上載者:User

摘要:需求是這樣的:當前節點是橫排的,但是搞了半天,發現它沒有這個屬性,也就索性用tk的控制項了,不是那種特別正式的MVVM的方式,但是還好吧,可以使用的

有一個bug:當所有的子節點不選擇的話,父節點還是選擇的

 

<UserControl x:Class="SilverlightApplication1.checkboxtree" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"   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"    mc:Ignorable="d"    d:DesignHeight="300" d:DesignWidth="400">    <UserControl.Resources>        <Style x:Key="RedItemStyle" TargetType="sdk:TreeViewItem">            <Setter Property="HeaderTemplate">                <Setter.Value>                    <DataTemplate>                        <StackPanel Orientation="Horizontal">                            <CheckBox IsChecked="{ Binding}"/>                            <TextBlock Text="{Binding }" Foreground="Red" FontStyle="Italic" />                        </StackPanel>                    </DataTemplate>                </Setter.Value>            </Setter>            <Setter Property="IsExpanded" Value="True" />        </Style>    </UserControl.Resources>    <Grid x:Name="LayoutRoot" Background="White">        <StackPanel x:Name="layout" Background="Azure" Orientation="Horizontal">            <sdk:TreeView x:Name="TreeOfLife" Margin="5" Grid.Column="0" Grid.Row="1"  SelectedItemChanged="TreeOfLife_SelectedItemChanged" >            </sdk:TreeView>        </StackPanel>    </Grid></UserControl>

 

using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using System.ComponentModel;using System.Collections.ObjectModel;using System.Windows.Data;namespace SilverlightApplication1{    public partial class checkboxtree : UserControl    {        ObservableCollection<ForumInfo> forumList = new ObservableCollection<ForumInfo>();//當前頁面的view        public checkboxtree()        {            InitializeComponent();            forumList = GetForumData();//得到資料來源            AddTreeNode(0, null);//將資料來源綁定到樹        }        private void TreeOfLife_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)        {        }        //使用遞迴的思想,將資料繫結到樹上        private void AddTreeNode(int parentID, TreeViewItem treeViewItem)        {            List<ForumInfo> result = (from forumInfo in forumList                                      where forumInfo.ParendID == parentID                                      select forumInfo).ToList<ForumInfo>();            if (result.Count > 0)            {                foreach (ForumInfo foruminfo in result)                {                    TreeViewItem objTreeNode = new TreeViewItem();                    CheckBox ck = new CheckBox();                    StackPanel sp = new StackPanel();                    CheckBox cb = new CheckBox();                    //當選擇父節點的時候選擇子節點                    cb.Checked += new RoutedEventHandler((sender, e) =>                     {                         foreach (TreeViewItem item in objTreeNode.Items)                         {                             ((item.Header as StackPanel).Children[0] as CheckBox).IsChecked = true;                         }                     });                    //不選擇父節點的時候,子幾點也不要選擇                    cb.Unchecked += new RoutedEventHandler((sender, e) =>                    {                        foreach (TreeViewItem item in objTreeNode.Items)                        {                            ((item.Header as StackPanel).Children[0] as CheckBox).IsChecked = false;                        }                    });                    cb.Content = foruminfo.ForumName;                    cb.IsChecked = foruminfo.IsChecked;                    sp.Children.Add(cb);                    objTreeNode.Header = sp;//關鍵就是在 這個 Header                                                          // 建立帶ck的節點等效與下面的代碼                    //<sdk:TreeViewItem>                    //    <sdk:TreeViewItem.Header>                    //        <CheckBox Content="我是葉子呀" Width="100"  Click="CheckBox_Click">                    //        </CheckBox>                    //    </sdk:TreeViewItem.Header>                    //</sdk:TreeViewItem>                    objTreeNode.DataContext = foruminfo;                    objTreeNode.IsExpanded = true;                    //objTreeNode.TabNavigation=                    //此樣式將會添加的所有葉子結點上                    //添加根節點                    if (treeViewItem == null)                    {                        TreeOfLife.Items.Add(objTreeNode);                    }                    else                    {                        treeViewItem.Items.Add(objTreeNode);                    }                    AddTreeNode(foruminfo.ForumID, objTreeNode);                }            }        }        #region  資料        public ObservableCollection<ForumInfo> GetForumData()        {            ObservableCollection<ForumInfo> forumList = new ObservableCollection<ForumInfo>();            forumList.Add(new ForumInfo() { ForumID = 1, ParendID = 0, ForumName = "根1", IsChecked = false });            forumList.Add(new ForumInfo() { ForumID = 2, ParendID = 1, ForumName = "子節點2", IsChecked = true });            forumList.Add(new ForumInfo() { ForumID = 3, ParendID = 1, ForumName = "子節點3", IsChecked = false });            forumList.Add(new ForumInfo() { ForumID = 4, ParendID = 1, ForumName = "子節點4", IsChecked = false });            forumList.Add(new ForumInfo() { ForumID = 5, ParendID = 4, ForumName = "子節點5", IsChecked = true });            forumList.Add(new ForumInfo() { ForumID = 6, ParendID = 4, ForumName = "子節點6", IsChecked = true });                     return forumList;        }        public class ForumInfo        {            public int ForumID { get; set; }            public int ParendID { get; set; }            public string ForumName { get; set; }            public bool IsChecked { get; set; }        }        #endregion    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.