码迷,mamicode.com
首页 > 其他好文 > 详细

TreeView

时间:2014-07-18 13:39:42      阅读:319      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   os   width   art   

  <!--子节点最后一个节点判断转换器-->
            <local:TreeViewLineConverter x:Key="LineConverter"/>

    class TreeViewLineConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            TreeViewItem item = (TreeViewItem)value;
            ItemsControl ic = ItemsControl.ItemsControlFromItemContainer(item);
            return ic.ItemContainerGenerator.IndexFromContainer(item) == ic.Items.Count - 1;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }

            <!--折叠按钮-->
            <Style x:Key="ExpandToggleButtonStyle" TargetType="{x:Type ToggleButton}">
                <Setter Property="Focusable" Value="False"/>
                <Setter Property="Margin" Value="0,5.5,0,0"></Setter>
                <Setter Property="Width" Value="19"/>
                <Setter Property="Height" Value="13"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ToggleButton}">
                            <Border Background="Transparent" Height="13" Width="19">
                                <Border BorderBrush="#999999" BorderThickness="1" Background="#333333" Height="9" SnapsToDevicePixels="true" Width="9">
                                    <Path x:Name="ExpandPath" 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" Fill="#999999" Margin="1,1,1,1"/>
                                </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>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Fill" TargetName="ExpandPath" Value="White"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <!--隐藏节点TreeViewItem-->
            <Style x:Key="TreeViewItemHiddenStyle" TargetType="{x:Type TreeViewItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" TargetType="{x:Type TreeViewItem}">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" MinWidth="19" />
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition />
                                </Grid.RowDefinitions>

                                <Border BorderThickness="{TemplateBinding Border.BorderThickness}"
                  Padding="{TemplateBinding Padding}"
                  BorderBrush="{TemplateBinding Border.BorderBrush}"
                  Background="{TemplateBinding Panel.Background}"
                  x:Name="Bd"
                  SnapsToDevicePixels="True"
                  Grid.Column="1">
                                    <ContentPresenter Content="{TemplateBinding HeaderedContentControl.Header}"
                   ContentTemplate="{TemplateBinding HeaderedContentControl.HeaderTemplate}"
                   ContentStringFormat="{TemplateBinding HeaderStringFormat}"
                   ContentSource="Header"
                   x:Name="PART_Header"
                   HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                   SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                                </Border>
                                <ItemsPresenter x:Name="ItemsHost" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="2" />
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsExpanded">
                                    <Trigger.Value>
                                        <s:Boolean>False</s:Boolean>
                                    </Trigger.Value>
                                    <Setter Property="Visibility" TargetName="ItemsHost" Value="{x:Static Visibility.Collapsed}"/>
                                </Trigger>
                                <Trigger Property="IsSelected">
                                    <Trigger.Value>
                                        <s:Boolean>True</s:Boolean>
                                    </Trigger.Value>
                                    <Setter Property="Panel.Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                                    <Setter Property="TextElement.Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                                </Trigger>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="IsSelected" Value="True"/>
                                        <Condition Property="Selector.IsSelectionActive" Value="False"/>
                                    </MultiTrigger.Conditions>
                                    <Setter Property="Panel.Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                                    <Setter Property="TextElement.Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                                </MultiTrigger>
                                <Trigger Property="IsEnabled" Value="False">
                                    <Setter Property="TextElement.Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <!--显示虚线样式TreeViewItem-->
            <Style x:Key="LineTreeViewItem"  TargetType="{x:Type TreeViewItem}">
                <Setter Property="Foreground" Value="#999999"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TreeViewItem}">
                            <Grid >
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition MinWidth="19" Width="Auto"/>
                                    <ColumnDefinition Width="auto"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition/>
                                </Grid.RowDefinitions>
                                <Rectangle  x:Name="HorLn" Height="1" Width="20"  Stroke="#999999"  Margin="12,5,0,0" SnapsToDevicePixels="true" StrokeDashArray="2" StrokeThickness="1"/>
                                <Rectangle x:Name="VerLn"  Width="1" Stroke="#999999" Margin="-10,1,0,0" Grid.RowSpan="2"  SnapsToDevicePixels="true" StrokeDashArray="2" StrokeThickness="1"  />
                                <ToggleButton x:Name="Expander"  Style="{StaticResource ExpandToggleButtonStyle}"  IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press"/>
                                <Border x:Name="Bd" Grid.Column="1"  Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                                    <ContentPresenter x:Name="PART_Header" Margin="0,5,0,0"  ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
                                </Border>
                                <ItemsPresenter x:Name="ItemsHost" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"/>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <DataTrigger  Binding="{Binding Converter={StaticResource LineConverter}, RelativeSource={RelativeSource Self}}" Value="true">
                                    <Setter TargetName="VerLn" Property="Height" Value="10"/>
                                    <Setter TargetName="VerLn" Property="VerticalAlignment" Value="Top"/>
                                    <!--<Setter TargetName="HorLn" Property="Margin" Value="12,5,0,0" />-->
                                </DataTrigger>
                                <Trigger Property="IsExpanded" Value="false">
                                    <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed"/>
                                </Trigger>
                                <Trigger Property="HasItems" Value="false">
                                    <Setter TargetName="Expander" Property="Visibility" Value="Collapsed"/>
                                </Trigger>
                                <Trigger Property="IsSelected" Value="true">
                                    <Setter Property="Foreground" Value="White"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <!--隐藏虚线样式-->
            <Style x:Key="NotLineTreeViewItem" TargetType="{x:Type TreeViewItem}" >
                <Setter Property="Foreground" Value="#999999"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TreeViewItem}">
                            <Grid >
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition MinWidth="19" Width="Auto"/>
                                    <ColumnDefinition Width="auto"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition/>
                                </Grid.RowDefinitions>
                                <Rectangle  x:Name="HorLn" Height="1" Width="20"  Stroke="#999999"  Margin="12,5,0,0" SnapsToDevicePixels="true" StrokeDashArray="2" StrokeThickness="1"/>
                                <Rectangle x:Name="VerLn" Width="1" Stroke="#999999" Margin="-10,1,0,0" Grid.RowSpan="2"  SnapsToDevicePixels="true" StrokeDashArray="2" StrokeThickness="1"  />
                                <ToggleButton x:Name="Expander"  Style="{StaticResource ExpandToggleButtonStyle}"  IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press"/>
                                <Border x:Name="Bd" Grid.Column="1"  Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                                    <ContentPresenter x:Name="PART_Header" Margin="0,5,0,0"  ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
                                </Border>
                                <ItemsPresenter x:Name="ItemsHost" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"/>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <DataTrigger  Binding="{Binding Converter={StaticResource LineConverter}, RelativeSource={RelativeSource Self}}" Value="true">
                                    <Setter TargetName="VerLn" Property="Height" Value="8"/>
                                    <Setter TargetName="VerLn" Property="VerticalAlignment" Value="Top"/>
                                    <Setter TargetName="HorLn" Property="Margin" Value="12,0,0,0" />
                                </DataTrigger>
                                <Trigger Property="IsExpanded" Value="false">
                                    <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed"/>
                                </Trigger>
                                <Trigger Property="HasItems" Value="false">
                                    <Setter TargetName="Expander" Property="Visibility" Value="Collapsed"/>
                                </Trigger>
                                <Trigger Property="IsExpanded" Value="true">
                                    <Setter TargetName="VerLn" Property="Visibility" Value="Collapsed"/>
                                    <Setter TargetName="HorLn" Property="Visibility" Value="Collapsed"/>
                                </Trigger>
                                <Trigger Property="IsExpanded" Value="false">
                                    <Setter TargetName="VerLn" Property="Visibility" Value="Collapsed"/>
                                    <Setter TargetName="HorLn" Property="Visibility" Value="Collapsed"/>
                                </Trigger>
                                <Trigger Property="IsSelected" Value="true">
                                    <Setter Property="Foreground" Value="White"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

 

 

                <Border Width="164"  Grid.Column="0" BorderThickness="4" CornerRadius="2"   BorderBrush="#666666" Margin="5">
                    <TreeView  BorderThickness="0" BorderBrush="Transparent" Background="#333333">
                        <TreeViewItem IsExpanded="True"  Style="{StaticResource TreeViewItemHiddenStyle}">
                            <TreeViewItem Header="A" Style="{StaticResource NotLineTreeViewItem}" />
                            <TreeViewItem Header="B" Style="{StaticResource NotLineTreeViewItem}" />
                            <TreeViewItem Header="C" Style="{StaticResource NotLineTreeViewItem}" />
                            <TreeViewItem Header="D" IsExpanded="True" Style="{StaticResource NotLineTreeViewItem}" >
                                <TreeViewItem Header="A" Style="{StaticResource LineTreeViewItem}"/>
                                <TreeViewItem Header="B" Style="{StaticResource LineTreeViewItem}"/>
                                <TreeViewItem Header="C" Style="{StaticResource LineTreeViewItem}"/>
                                <TreeViewItem Header="D" Style="{StaticResource LineTreeViewItem}"/>
                                <TreeViewItem Header="E" Style="{StaticResource LineTreeViewItem}"/>
                            </TreeViewItem>
                            <TreeViewItem Header="E" Style="{StaticResource NotLineTreeViewItem}">
                                <TreeViewItem Header="A"   Style="{StaticResource LineTreeViewItem}" />
                                <TreeViewItem Header="B   Style="{StaticResource LineTreeViewItem}" />
                            </TreeViewItem>
                            <TreeViewItem Header="F" Style="{StaticResource NotLineTreeViewItem}">
                                <TreeViewItem Header="A"   Style="{StaticResource LineTreeViewItem}" />
                                <TreeViewItem Header="B"   Style="{StaticResource LineTreeViewItem}" />
                            </TreeViewItem>
                        </TreeViewItem>
                    </TreeView>
                </Border>

TreeView,布布扣,bubuko.com

TreeView

标签:style   http   color   os   width   art   

原文地址:http://www.cnblogs.com/hidden/p/3852957.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!