Wpf-Treeview

时间:2014-04-30 00:02:06   收藏:0   阅读:376

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

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!