当前位置:编程学习 > C#/ASP.NET >>

WPF TreeGrid(树形展开的表格)

一直都想要个树形展开的表格 像这样的

 

今天心血来潮就简单做了个TreeGrid 喜欢的同学可以下载下去自己研究下,其实也比较简单主要就是TreeView TreeViewItem再配合GridViewHeaderRowPresenter、GridViewRowPresenter、GridViewColumnCollection定制style基本上就可以实现以上效果

本文中涉及大量模板,绑定等知识,如果刚刚入门还没有了解以上知识的同学可以先补下课,然后再来看,以免浪费时间。

闲话不多说,直接上代码:

前台代码 MainWindow.xaml

[csharp] 
<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

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,