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 控件
66 1
|
数据处理 C#
C#-TreeView
TreeView是一个树形结构的控件,能够表现出对象的层级关系,比如文件夹目录展示经常使用。
108 0
|
存储 C#
C#(三十)之C#comboBox ListView treeView
本篇内容记录了comboBox下拉列表框属性、ListView 列表视图属性、ListView 列表视图事件、treeView树状视图属性。
271 0
C#(三十)之C#comboBox ListView treeView
WinForm——TreeView总结
WinForm——TreeView总结
453 0
ListView、TreeView和DataGrid。
原文:ListView、TreeView和DataGrid。 1、ListView。 ListView继承自简单的没有特色的ListBox,并使用View属性进行扩展。增加了对基于列显示的支持,并增加了快速切换视图或显示模式的能力,而不需要重新绑定数据以及重新构建列表。
1246 0