wpf treeview


首先是xaml代碼:

<TreeView Grid.Row="1" Name="tvProperty">
            <TreeView.Background>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FFF05F5F" Offset="0"/>
                            <GradientStop Color="#FFF0A3A3" Offset="1"/>
                        </LinearGradientBrush>
                    </TreeView.Background>
                    <TreeView.ItemTemplate>
                        <HierarchicalDataTemplate DataType="{x:Type local:PropertyNodeItem}" ItemsSource="{Binding Path=Children}">
                            <StackPanel Orientation="Horizontal">
                                <Image VerticalAlignment="Center" Source="{Binding Icon}" Width="16px" Height="16px" Margin="0,0,2,2"/>
                                <TextBlock VerticalAlignment="Center" Text="{Binding DisplayName}" Foreground="White" FontSize="14"/>
                                <StackPanel.ToolTip>
                                    <TextBlock VerticalAlignment="Center" Text="{Binding Name}" TextWrapping="Wrap" MaxWidth="200px" FontSize="14"/>
                                </StackPanel.ToolTip>
                            </StackPanel>
                        </HierarchicalDataTemplate>
                    </TreeView.ItemTemplate> </TreeView>

建一個PropertyNodeItem類:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RunTrack.model
{
    internal class PropertyNodeItem
    {
        public string Icon { get; set; }
        public string DisplayName { get; set; }
        public string Name { get; set; }
        public List<PropertyNodeItem> Children { get; set; }
        public PropertyNodeItem()
        {
            Children = new List<PropertyNodeItem>();
        }
    }
}

 在xaml.cs文件中添加方法:

private void ShowTreeView()
        {
            List<PropertyNodeItem> listItem = new List<PropertyNodeItem>();
            PropertyNodeItem mainNode = new PropertyNodeItem() 
            {
                Icon=FLODER_ICON,
                DisplayName="功能菜單",
                Name="主目錄--功能菜單"
            };
            
            PropertyNodeItem systemNode = new PropertyNodeItem() 
            {
                Icon = FLODER_ICON,
                DisplayName="系統設置",
                Name="當前菜單--系統設置"
            };
            PropertyNodeItem pwdTag = new PropertyNodeItem() 
            {
                Icon=LEAF_ICON,
                DisplayName="密碼修改",
                Name="當前選項--密碼修改"
            };
            systemNode.Children.Add(pwdTag);
            mainNode.Children.Add(systemNode);
            listItem.Add(mainNode);
            this.tvProperty.ItemsSource = listItem;
        }

 窗體初始化時調用改方法,便能顯示出樹狀菜單


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM