Wpf-Treeview

1.xaml

 xmlns:local="clr-namespace:YaoMinSys.Class"
 <TreeView Name="Treeview"  Padding="0" Margin="5" BorderThickness="1">
                        <TreeView.ItemContainerStyle>
                            <Style TargetType="TreeViewItem">
                                <Setter Property="IsExpanded" Value="True"></Setter>
                            </Style>
                        </TreeView.ItemContainerStyle>
                        <TreeView.ItemTemplate>
                            <HierarchicalDataTemplate DataType="{x:Type local:PropertyNodeItem}" ItemsSource="{Binding Path=Children}" >
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock VerticalAlignment="Center" Text="{Binding Path=DisplayName}" Tag="{Binding Path=ID}" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown"></TextBlock>
                                </StackPanel>
                            </HierarchicalDataTemplate>
                        </TreeView.ItemTemplate>
                    </TreeView>

2.class

namespace YaoMinSys.Class
{
    class PropertyNodeItem
    {
        public int ID { get; set; }
        public string DisplayName { get; set; }

        public List<PropertyNodeItem> Children { get; set; }

        public PropertyNodeItem()
        {
            Children = new List<PropertyNodeItem>();
        }
    }
}

3.xaml.cs

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            ShowTreeView();
        }
        private void ShowTreeView()
        {
            List<PropertyNodeItem> itemlist = new List<PropertyNodeItem>();
            try
            {
                YaoMinSys.BLL.ZiDianLei ZDLBll = new BLL.ZiDianLei();
                DataSet ZDLDs = ZDLBll.GetList("");
                for (int i = 0; i < ZDLDs.Tables[0].Rows.Count; i++)
                {
                    PropertyNodeItem Parent = new PropertyNodeItem
                    {
                        ID = int.Parse(ZDLDs.Tables[0].Rows[i]["ZDLID"].ToString()),
                        DisplayName = ZDLDs.Tables[0].Rows[i]["ZDLeiMing"].ToString(),
                        
                    };
                   
                    YaoMinSys.BLL.ZiDianXiang ZDXBll = new BLL.ZiDianXiang();
                    DataSet ZDXDs = ZDXBll.GetList(" ZDLID = " + ZDLDs.Tables[0].Rows[i]["ZDLID"]);
                    for (int j = 0; j < ZDXDs.Tables[0].Rows.Count; j++)
                    {
                        PropertyNodeItem children = new PropertyNodeItem
                        {
                            ID = int.Parse(ZDXDs.Tables[0].Rows[j]["ZDXID"].ToString()),
                            DisplayName = ZDXDs.Tables[0].Rows[j]["ZDXiang"].ToString()
                        };
                        Parent.Children.Add(children);
                    }
                    itemlist.Add(Parent);
                }
                this.Treeview.ItemsSource = itemlist;
                
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            TextBlock tb = (TextBlock)sender;
            MessageBox.Show("Text :"+tb.Text+"ID :"+tb.Tag);
        }
posted @ 2014-04-28 17:02  咲丶  阅读(196)  评论(0编辑  收藏  举报