首页 > 代码库 > wpf treeview
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类:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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文件中添加方法:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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; } |
窗体初始化时调用改方法,便能显示出树状菜单
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。