WPF 可以自定义字段的Treeview

来源:互联网 发布:国外手机安全软件 编辑:程序博客网 时间:2024/06/16 02:08
        <TreeView x:Name="tvProperties" Width="250" Padding="0" Margin="20.5,8,20.5,-8" BorderThickness="1">            <TreeView.ItemTemplate>                <HierarchicalDataTemplate DataType="{x:Type local:PropertyNodeItem}" ItemsSource="{Binding Children}">                    <StackPanel Orientation="Horizontal">                        <StackPanel.ToolTip>                            <TextBlock VerticalAlignment="Center" Text="{Binding Name}" TextWrapping="Wrap" MaxWidth="200" />                        </StackPanel.ToolTip>                        <Image VerticalAlignment="Center" Source="{Binding Icon}" Width="16" Height="16" Margin="0,0,2,2"/>                        <TextBlock VerticalAlignment="Center" Text="{Binding DisplayName}"/>                        <Image VerticalAlignment="Center" Source="{Binding EditIcon}" Width="16" Height="16" Margin="2,0,0,0"/>                    </StackPanel>                </HierarchicalDataTemplate>            </TreeView.ItemTemplate>        </TreeView>




private string FOLDER_ICON=@"Image/33.png";        private string TAG_ICON=@"Image/31.png";        private string EDITABLE_ICON=@"Image/6.png";        private void ShowTreeView()        {            List<PropertyNodeItem> itemList = new List<PropertyNodeItem>();            PropertyNodeItem node1 = new PropertyNodeItem()            {                DisplayName = "Node No.1",                Name = "This is the discription of Node1. This is a folder.",                Icon = FOLDER_ICON,            };            PropertyNodeItem node1tag1 = new PropertyNodeItem()            {                DisplayName = "Tag No.1",                Name = "This is the discription of Tag 1. This is a tag.",                Icon = TAG_ICON,                EditIcon = EDITABLE_ICON            };            node1.Children.Add(node1tag1);            PropertyNodeItem node1tag2 = new PropertyNodeItem()            {                DisplayName = "Tag No.2",                Name = "This is the discription of Tag 2. This is a tag.",                Icon = TAG_ICON,                EditIcon = EDITABLE_ICON            };            node1.Children.Add(node1tag2);            itemList.Add(node1);            PropertyNodeItem node2 = new PropertyNodeItem()            {                DisplayName = "Node No.2",                Name = "This is the discription of Node 2. This is a folder.",                Icon = FOLDER_ICON,            };            PropertyNodeItem node2tag3 = new PropertyNodeItem()            {                DisplayName = "Tag No.3",                Name = "This is the discription of Tag 3. This is a tag.",                Icon = TAG_ICON,                EditIcon = EDITABLE_ICON            };            node2.Children.Add(node2tag3);            PropertyNodeItem node2tag4 = new PropertyNodeItem()            {                DisplayName = "Tag No.4",                Name = "This is the discription of Tag 4. This is a tag.",                Icon = TAG_ICON,                EditIcon = EDITABLE_ICON            };            node2.Children.Add(node2tag4);            itemList.Add(node2);            this.tvProperties.ItemsSource = itemList;        }    }
//TreeView节点定义    internal class PropertyNodeItem    {        public string Icon { get; set; }        public string EditIcon { get; set; }        public string DisplayName { get; set; }        public string Name { get; set; }        public List<PropertyNodeItem> Children { get; set; }        public PropertyNodeItem()        {            Children = new List<PropertyNodeItem>();        }    }


原创粉丝点击