摘要:需求是这样的:当前节点是横排的,但是搞了半天,发现它没有这个属性,也就索性用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
- }
- }
转自:http://blog.csdn.net/caikundashu/article/details/7434205