摘要:需求是這樣的:當前節點是橫排的,但是搞了半天,發現它沒有這個屬性,也就索性用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 }}