赞
踩
一直都想要个树形展开的表格 像这样的
今天心血来潮就简单做了个TreeGrid 喜欢的同学可以下载下去自己研究下,其实也比较简单主要就是TreeView TreeViewItem再配合GridViewHeaderRowPresenter、GridViewRowPresenter、GridViewColumnCollection定制style基本上就可以实现以上效果
本文中涉及大量模板,绑定等知识,如果刚刚入门还没有了解以上知识的同学可以先补下课,然后再来看,以免浪费时间。
闲话不多说,直接上代码:
前台代码 MainWindow.xaml
- <Window x:Class="TreeGrid.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:TreeGrid"
- Title="MainWindow" Height="350" Width="525">
- <Window.Resources>
- <local:LevelToMarginConverter x:Key="LevelToIndentConverter"/>
-
- <Style x:Key="ExpandCollapseToggleStyle"
- TargetType="{x:Type ToggleButton}">
- <Setter Property="Focusable"
- Value="False"/>
- <Setter Property="Width"
- Value="19"/>
- <Setter Property="Height"
- Value="13"/>
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type ToggleButton}">
- <Border Width="19"
- Height="13"
- Background="Transparent">
- <Border Width="9"
- Height="9"
- BorderThickness="1"
- BorderBrush="#FF7898B5"
- CornerRadius="1"
- SnapsToDevicePixels="true">
- <Border.Background>
- <LinearGradientBrush StartPoint="0,0"
- EndPoint="1,1">
- <LinearGradientBrush.GradientStops>
- <GradientStop Color="White"
- Offset=".2"/>
- <GradientStop Color="#FFC0B7A6"
- Offset="1"/>
- </LinearGradientBrush.GradientStops>
- </LinearGradientBrush>
- </Border.Background>
- <Path x:Name="ExpandPath"
- Margin="1,1,1,1"
- Fill="Black"
- Data="M 0 2 L 0 3 L 2 3 L 2 5 L 3 5 L 3 3
- L 5 3 L 5 2 L 3 2 L 3 0 L 2 0 L 2 2 Z"/>
- </Border>
- </Border>
- <ControlTemplate.Triggers>
- <Trigger Property="IsChecked"
- Value="True">
- <Setter Property="Data"
- TargetName="ExpandPath"
- Value="M 0 2 L 0 3 L 5 3 L 5 2 Z"/>
- </Trigger>
- </ControlTemplate.Triggers>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
-
- <DataTemplate x:Key="CellTemplate_Name">
- <DockPanel>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。