Silverlight学习笔记六TreeView控件

这一节是TreeView控件的学习

下面是个例子

1.TreeViewSample.xaml

<UserControl xmlns:sdk="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"  x:Class="Silverlight.Common.View.TreeViewSample"
    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"      
xmlns:common="clr-namespace:System.Windows;assembly=System.Windows.Controls"
    mc:Ignorable="d"
    d:DesignHeight="400" d:DesignWidth="1100">

    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition  Height="400"/>
            <RowDefinition Height="25"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition  Width="200"/>
            <ColumnDefinition Width="200"/>
            <ColumnDefinition Width="200"/>
        </Grid.ColumnDefinitions>
        <sdk:TreeView >
            <sdk:TreeViewItem Header="TreeViewItem1"></sdk:TreeViewItem>
            <sdk:TreeViewItem Header="TreeViewItem2">
                <sdk:TreeViewItem Header="TreeViewItemA"></sdk:TreeViewItem>
                <sdk:TreeViewItem Header="TreeViewItemB">
                    <sdk:TreeViewItem Header="TreeViewItemC" IsEnabled="False"></sdk:TreeViewItem>
                </sdk:TreeViewItem>
            </sdk:TreeViewItem>
        </sdk:TreeView>

        <sdk:TreeView Grid.Column="1" x:Name="t1">
            <sdk:TreeViewItem Header="TreeViewItem1" x:Name="ti1">
                <Button Content="按钮1"></Button>
                <TextBox Text="文本框1"></TextBox>
                <CheckBox IsChecked="True"></CheckBox>
            </sdk:TreeViewItem>
            <sdk:TreeViewItem Header="TreeViewItem2"  x:Name="ti2">
                <sdk:TreeViewItem Header="TreeViewItemA">
                    <Button Content="按钮2"></Button>
                    <TextBox Text="文本框2"></TextBox>
                    <RadioButton IsChecked="True"></RadioButton>
                    <RadioButton ></RadioButton>
                </sdk:TreeViewItem>
                <sdk:TreeViewItem Header="TreeViewItemB">
                    <sdk:TreeViewItem Header="TreeViewItemC" IsEnabled="False"></sdk:TreeViewItem>
                </sdk:TreeViewItem>
            </sdk:TreeViewItem>
        </sdk:TreeView>

        <sdk:TreeView  x:Name="treeView" Grid.Column="2" SelectedItemChanged="treeView_SelectedItemChanged"/>
     
        <TextBlock x:Name="tb1" Text="所选项:" HorizontalAlignment="Right" Grid.Column="1" Grid.Row="1"></TextBlock>
        <ContentControl x:Name="SelectedItem" FontWeight="Bold" Grid.Column="2" Grid.Row="1" VerticalAlignment="Center" />
        <TextBlock x:Name="tb2" Text="所选值:" HorizontalAlignment="Right" Grid.Column="1" Grid.Row="2"></TextBlock>
        <ContentControl x:Name="SelectedValue" FontWeight="Bold" VerticalAlignment="Center"  Grid.Column="2" Grid.Row="2"/>

        <CheckBox x:Name="cbExpandAll" Grid.Column="1" Grid.Row="1"  Content="IsExpand" Checked="cbExpandAll_Checked"></CheckBox>
    </Grid>
</UserControl>

 

2.TreeViewSample.cs

 public partial class TreeViewSample : UserControl
    {
        public TreeViewSample()
        {
            InitializeComponent();

    //动态节点
            this.AddTreeNode(0,null);
        }

        private void treeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeView view = sender as TreeView;
        }

        private void cbExpandAll_Checked(object sender, RoutedEventArgs e)
        {
        
                this.ti1.IsExpanded = true;
                this.ti2.IsExpanded = true;
           
        }

        IList<Area> areaList = new Area().DataSource();

    //动态添加节点

        private void AddTreeNode(int parentID, TreeViewItem treeViewItem)
        {
            IList<Area> tree = (from li in areaList
                                where li.ParentID == parentID
                                select li
                                  ).ToList<Area>();

            if (tree.Count > 0)
            {
                foreach (Area area in tree)
                {
                    TreeViewItem objTreeNode = new TreeViewItem();
                    objTreeNode.Header = area.Name;
                    objTreeNode.DataContext = area;
                    objTreeNode.IsExpanded = true;

                    if (treeViewItem == null)
                    {
                        treeView.Items.Add(objTreeNode);
                    }
                    else
                    {
                        treeViewItem.Items.Add(objTreeNode);
                    }
                    AddTreeNode(area.AreaID, objTreeNode);
                }
            }
        }

         }

 

3.实体

 public class Area
    {
        #region AreaID

        /// <summary>
        /// Field:AreaID 地区ID
        /// </summary>
        public virtual int AreaID { get; set; }

        #endregion

        #region ParentID

        /// <summary>
        /// Field:ParentID 父地区ID
        /// </summary>
        public virtual int ParentID { get; set; }

        #endregion

        #region Name

        /// <summary>
        /// Field:Name 地区名
        /// </summary>
        public virtual string Name { get; set; }

        #endregion

        public IList<Area> DataSource()
        {
            IList<Area> list = new List<Area>();

            list.Add(new Area() { AreaID = 1, ParentID = 0, Name = "地球" });
            list.Add(new Area() { AreaID = 2, ParentID = 1, Name = "中国" });
            list.Add(new Area() { AreaID = 3, ParentID = 1, Name = "韩国" });
            list.Add(new Area() { AreaID = 4, ParentID = 1, Name = "美国" });
            list.Add(new Area() { AreaID = 5, ParentID = 2, Name = "北京" });
            list.Add(new Area() { AreaID = 6, ParentID = 2, Name = "上海" });
            list.Add(new Area() { AreaID = 7, ParentID = 2, Name = "山西" });
            list.Add(new Area() { AreaID = 8, ParentID = 7, Name = "太原" });
            list.Add(new Area() { AreaID = 9, ParentID = 7, Name = "忻州" });
            list.Add(new Area() { AreaID = 10, ParentID = 9, Name = "神驰" });
            list.Add(new Area() { AreaID = 11, ParentID = 3, Name = "首尔" });
            list.Add(new Area() { AreaID = 12, ParentID = 3, Name = "釜山" });
            list.Add(new Area() { AreaID = 13, ParentID = 3, Name = "济州" });
            list.Add(new Area() { AreaID = 14, ParentID = 13, Name = "济州岛" });
            list.Add(new Area() { AreaID = 15, ParentID = 4, Name = "纽约" });

            return list;
        }
    }

注:ContentControl是具有Content属性控件的父类,它的Content属性返回它子类的Content值

  IsExpanded是否展开

  SelectedItemChanged是TreeView的一个重要事件,在选择item时发生。

posted @ 2010-07-12 12:33  ForrestWoo  阅读(6437)  评论(2编辑  收藏  举报