TreeView

简介:





例子中给了五个树形菜单,第一个是简单的树形菜单,第二个是嵌套了按钮的树形菜单,第三个是父级是按钮,第四个是绑定了资源的树形,第五个是嵌套了图片的树形。

<Window x:Class="SimpleTreeView.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="SimpleTreeView"
    >
 <Window.Resources>
    <Style TargetType="{x:Type TreeViewItem}">
      <Setter Property="Foreground" Value="Blue"/>
      <Setter Property="FontSize" Value="12"/>
    </Style>
 
    <Style TargetType="{x:Type TextBox}">
      <Setter Property="Width" Value="80"/>
      <Setter Property="TextWrapping" Value="Wrap"/>
    </Style>
    <XmlDataProvider x:Key="myEmployeeData" XPath="/EmployeeData">
      <x:XData>
        <EmployeeData xmlns="">
          <EmployeeInfo>
            <EmployeeInfoData>Employee1</EmployeeInfoData>
            <Item Type="FirstName"> 桂素伟 </Item>
            <Item Type="LastName"> 刘靖凯 </Item>
            <Item Type="EmployeeNumber"> 张三 </Item>
          </EmployeeInfo>
          <EmployeeInfo>
            <EmployeeInfoData>Employee2</EmployeeInfoData>
            <Item Type="FirstName"> 王五 </Item>
            <Item Type="LastName"> 赵六 </Item>
            <Item Type="EmployeeNumber"> 李四 </Item>
          </EmployeeInfo>
        </EmployeeData>
      </x:XData>
    </XmlDataProvider>  
 <HierarchicalDataTemplate DataType="EmployeeInfo"
      ItemsSource ="{Binding XPath=Item}">
      <TextBlock Text="{Binding XPath=EmployeeInfoData}" />
    </HierarchicalDataTemplate>
 
 </Window.Resources>
    <StackPanel>    
       <TextBlock>
         简单的树形 </TextBlock>
      <TreeView>
        <TreeViewItem Header="Employee1">
          <TreeViewItem Header="Jesper"/>
          <TreeViewItem Header="Aaberg"/>
          <TreeViewItem Header="12345"/>
        </TreeViewItem>
       <TreeViewItem Header="Employee2">
          <TreeViewItem Header="Dominik"/>
          <TreeViewItem Header="Paiha"/>
          <TreeViewItem Header="98765"/>
        </TreeViewItem>
      </TreeView>
 
        <TextBlock>
         组合树形 1
      </TextBlock>
        <TreeView>
          <TreeViewItem Header ="Employee1">
            <TreeViewItem.Items>
              <Button>Jesper</Button>
              <Button>Aaberg</Button>
              <Button>12345</Button>
            </TreeViewItem.Items>
          </TreeViewItem>
          <TreeViewItem Header="Employee2">
            <TreeViewItem.Items>
              <Button>Dominik</Button>
              <Button>Paiha</Button>
              <Button>98765</Button>
            </TreeViewItem.Items>
          </TreeViewItem>
        </TreeView>
        <TextBlock>
        组合树形 2
      </TextBlock>
     <TreeView>
        <TreeViewItem>
          <TreeViewItem.Header>
            <Button>Employee1</Button>
          </TreeViewItem.Header>
          <TreeViewItem Header="Jesper"/>
          <TreeViewItem Header="Aaberg"/>
          <TreeViewItem Header="12345"/>
        </TreeViewItem>
        <TreeViewItem>
          <TreeViewItem.Header>
            <Button>Employee2</Button>
          </TreeViewItem.Header>
          <TreeViewItem Header="Dominik"/>
          <TreeViewItem Header="Paiha"/>
          <TreeViewItem Header="98765"/>
        </TreeViewItem>
      </TreeView>
        <TextBlock>
         资源绑定的树形
      </TextBlock>
      <TreeView ItemsSource="{Binding Source={StaticResource myEmployeeData},
        XPath=EmployeeInfo}"/>
      <TextBlock>
        组合树形三
      </TextBlock>
      <TreeView>
        <TreeViewItem Header="Animals">
          <TreeViewItem.Items>
          <DockPanel>
            <Image Source="data\fish.png"/>
            <TextBlock Margin="5" Foreground="Brown"
                       FontSize="12">Fish</TextBlock>
          </DockPanel>
          <DockPanel>
            <Image Source="data\dog.png"/>
            <TextBlock Margin="5" Foreground="Brown"
                       FontSize="12">Dog</TextBlock>
          </DockPanel>
          <DockPanel>
            <Image Source="data\cat.png"/>
            <TextBlock Margin="5" Foreground="Brown"
                       FontSize="12">Cat</TextBlock>
          </DockPanel>
        </TreeViewItem.Items>
        </TreeViewItem>     
      </TreeView>
 
    </StackPanel>
</Window>
















本文转自桂素伟51CTO博客,原文链接:http://blog.51cto.com/axzxs/150005 ,如需转载请自行联系原作者

相关文章
|
6月前
|
C# 数据库
40.c#:TreeView 控件
40.c#:TreeView 控件
64 1
|
数据处理 C#
C#-TreeView
TreeView是一个树形结构的控件,能够表现出对象的层级关系,比如文件夹目录展示经常使用。
106 0
WinForm——TreeView总结
WinForm——TreeView总结
450 0
|
C#
Seaching TreeVIew WPF
原文:Seaching TreeVIew WPF 项目中有一个树形结构的资源,需要支持搜索功能,搜索出来的结果还是需要按照树形结构展示,下面是简单实现的demo。 1.首先创建TreeViewItem的ViewModel,一般情况下,树形结构都包含DisplayName,Deepth,Parent...
834 0
ListView、TreeView和DataGrid。
原文:ListView、TreeView和DataGrid。 1、ListView。 ListView继承自简单的没有特色的ListBox,并使用View属性进行扩展。增加了对基于列显示的支持,并增加了快速切换视图或显示模式的能力,而不需要重新绑定数据以及重新构建列表。
1243 0
|
C#
WPF TreeView HierarchicalDataTemplate
原文 WPF TreeView HierarchicalDataTemplate   ...
1666 0