码迷,mamicode.com
首页 > 其他好文 > 详细

Wpf-Treeview

时间:2014-04-30 00:02:06      阅读:376      评论:0      收藏:0      [点我收藏+]

标签:com   http   blog   style   class   div   img   code   java   javascript   tar   

1.xaml

 xmlns:local="clr-namespace:YaoMinSys.Class"
bubuko.com,布布扣
 <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>
bubuko.com,布布扣

2.calss

bubuko.com,布布扣
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>();
        }
    }
}
bubuko.com,布布扣

3.xaml.cs

bubuko.com,布布扣
        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);
        }
bubuko.com,布布扣

Wpf-Treeview,布布扣,bubuko.com

Wpf-Treeview

标签:com   http   blog   style   class   div   img   code   java   javascript   tar   

原文地址:http://www.cnblogs.com/guo-pc31/p/3696802.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!