zoukankan      html  css  js  c++  java
  • silverlight 中treeview初探

    1.TreeViewSample.xaml

    View Code
    <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

    View Code
    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.实体

    View Code
     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;
            }
        }

    HierarchicalDataTemplate使用的例子

    View Code
     <UserControl.Resources>
            <sdk:HierarchicalDataTemplate x:Key="CityTemplate">
                <StackPanel>
                    <TextBlock Text="{Binding Name}"/>
                </StackPanel>
            </sdk:HierarchicalDataTemplate>
            <sdk:HierarchicalDataTemplate x:Key="ProvinceTemplate" ItemsSource="{Binding Citys}" 
                                          ItemTemplate="{StaticResource CityTemplate}">
                <StackPanel>
                    <TextBlock Text="{Binding Name}" Foreground="Green"/>
                </StackPanel>
            </sdk:HierarchicalDataTemplate>
            <sdk:HierarchicalDataTemplate x:Key="CountryTemplate" ItemsSource="{Binding Privinces}" 
                                          ItemTemplate="{StaticResource ProvinceTemplate}">
                <TextBlock Text="{Binding Name}" Foreground="Blue"/>
            </sdk:HierarchicalDataTemplate>
        </UserControl.Resources>
        <Grid x:Name="LayoutRoot" Background="White" >
    
            <sdk:TreeView x:Name="tvDemo" ItemTemplate="{StaticResource CountryTemplate}">
    
            </sdk:TreeView>

    后台

    View Code
      public MainPage()
            {
                InitializeComponent();
                tvDemo.ItemsSource = new ObservableCollection<Country> { 
                  new Country { 
                      Name = "中国", 
                      Privinces = { new Province 
                        { 
                          Name="山东省",
                          Citys = { 
                              new City { Name = "济南市" },
                              new City { Name= "淄博市" }
                         }
                       },
                     new Province 
                       { 
                         Name="广东省", 
                         Citys = { 
                             new City { Name = "广州市" },
                             new City { Name= "佛山市" }
                         }
                       }
                    }
                },
                 new Country { 
                     Name = "加拿大", 
                    Privinces = { new Province 
                      { 
                         Name="哥伦比亚省",
                         Citys = { 
                             new City { Name = "温哥华市" },
                             new City { Name= "维多利亚市" }
                         }
                       },
                     new Province 
                       { 
                         Name="阿尔伯塔省", 
                         Citys = { 
                             new City { Name = "埃德蒙顿市" },
                             new City { Name= "卡尔加里市" }
                         }
                       }
                   }            }
             };
            }
        }
        public class Country
        {
            public Country()
            {
                Privinces = new ObservableCollection<Province>();
            }
    
            public string Name { get; set; }
            public ObservableCollection<Province> Privinces { get; set; }
        }
    
        public class Province
        {
            public Province()
            {
                Citys = new ObservableCollection<City>();
            }
    
            public string Name { get; set; }
            public ObservableCollection<City> Citys { get; set; }
        }
    
        public class City
        {
            public string Name { get; set; }
        }
    }
  • 相关阅读:
    Linux文件系统
    Linux用户和用户管理
    Linux磁盘管理
    vi编辑器
    Linux常用命令
    Linux进程管理
    servlet运行过程
    http的get和post方法的区别
    千元机小荐
    JavaScript 正则表达式(Reg Exp)
  • 原文地址:https://www.cnblogs.com/akingyao/p/3075277.html
Copyright © 2011-2022 走看看