Advertisement
Guest User

Styling treeview

a guest
Nov 25th, 2013
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
XML 14.06 KB | None | 0 0
  1. <Window x:Class="WpfSketchbook.MainWindow"
  2.        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.        xmlns:local="clr-namespace:WpfSketchbook"
  5.        Title="MainWindow" Height="350" Width="525">
  6.     <Window.Resources>
  7.         <SolidColorBrush x:Key="ListBorder" Color="Red"/>
  8.         <Style x:Key="TreeViewStyle1" TargetType="{x:Type TreeView}">
  9.             <Style.Resources>
  10.                 <Style x:Key="TreeViewItemFocusVisual">
  11.                     <Setter Property="Control.Template">
  12.                         <Setter.Value>
  13.                             <ControlTemplate>
  14.                                 <Rectangle/>
  15.                             </ControlTemplate>
  16.                         </Setter.Value>
  17.                     </Setter>
  18.                 </Style>
  19.                 <SolidColorBrush x:Key="TreeViewItem.TreeArrow.Static.Checked.Fill" Color="#FF595959"/>
  20.                 <SolidColorBrush x:Key="TreeViewItem.TreeArrow.Static.Checked.Stroke" Color="#FF262626"/>
  21.                 <SolidColorBrush x:Key="TreeViewItem.TreeArrow.MouseOver.Stroke" Color="#FF27C7F7"/>
  22.                 <SolidColorBrush x:Key="TreeViewItem.TreeArrow.MouseOver.Fill" Color="#FFCCEEFB"/>
  23.                 <SolidColorBrush x:Key="TreeViewItem.TreeArrow.MouseOver.Checked.Stroke" Color="#FF1CC4F7"/>
  24.                 <SolidColorBrush x:Key="TreeViewItem.TreeArrow.MouseOver.Checked.Fill" Color="#FF82DFFB"/>
  25.                 <PathGeometry x:Key="TreeArrow" Figures="M0,0 L0,6 L6,0 z"/>
  26.                 <SolidColorBrush x:Key="TreeViewItem.TreeArrow.Static.Fill" Color="Red"/>
  27.                 <SolidColorBrush x:Key="TreeViewItem.TreeArrow.Static.Stroke" Color="Blue"/>
  28.                 <Style x:Key="ExpandCollapseToggleStyle" TargetType="{x:Type ToggleButton}">
  29.                     <Setter Property="Focusable" Value="False"/>
  30.                     <Setter Property="Width" Value="16"/>
  31.                     <Setter Property="Height" Value="16"/>
  32.                     <Setter Property="Template">
  33.                         <Setter.Value>
  34.                             <ControlTemplate TargetType="{x:Type ToggleButton}">
  35.                                 <Border Background="Transparent" Height="16" Padding="5,5,5,5" Width="16">
  36.                                     <Path x:Name="ExpandPath" Data="{StaticResource TreeArrow}" Fill="{StaticResource TreeViewItem.TreeArrow.Static.Fill}" Stroke="{StaticResource TreeViewItem.TreeArrow.Static.Stroke}">
  37.                                         <Path.RenderTransform>
  38.                                             <RotateTransform Angle="135" CenterY="3" CenterX="3"/>
  39.                                         </Path.RenderTransform>
  40.                                     </Path>
  41.                                 </Border>
  42.                                 <ControlTemplate.Triggers>
  43.                                     <Trigger Property="IsChecked" Value="True">
  44.                                         <Setter Property="RenderTransform" TargetName="ExpandPath">
  45.                                             <Setter.Value>
  46.                                                 <RotateTransform Angle="180" CenterY="3" CenterX="3"/>
  47.                                             </Setter.Value>
  48.                                         </Setter>
  49.                                         <Setter Property="Fill" TargetName="ExpandPath" Value="{StaticResource TreeViewItem.TreeArrow.Static.Checked.Fill}"/>
  50.                                         <Setter Property="Stroke" TargetName="ExpandPath" Value="{StaticResource TreeViewItem.TreeArrow.Static.Checked.Stroke}"/>
  51.                                     </Trigger>
  52.                                     <Trigger Property="IsMouseOver" Value="True">
  53.                                         <Setter Property="Stroke" TargetName="ExpandPath" Value="{StaticResource TreeViewItem.TreeArrow.MouseOver.Stroke}"/>
  54.                                         <Setter Property="Fill" TargetName="ExpandPath" Value="{StaticResource TreeViewItem.TreeArrow.MouseOver.Fill}"/>
  55.                                     </Trigger>
  56.                                     <MultiTrigger>
  57.                                         <MultiTrigger.Conditions>
  58.                                             <Condition Property="IsMouseOver" Value="True"/>
  59.                                             <Condition Property="IsChecked" Value="True"/>
  60.                                         </MultiTrigger.Conditions>
  61.                                         <Setter Property="Stroke" TargetName="ExpandPath" Value="{StaticResource TreeViewItem.TreeArrow.MouseOver.Checked.Stroke}"/>
  62.                                         <Setter Property="Fill" TargetName="ExpandPath" Value="{StaticResource TreeViewItem.TreeArrow.MouseOver.Checked.Fill}"/>
  63.                                     </MultiTrigger>
  64.                                 </ControlTemplate.Triggers>
  65.                             </ControlTemplate>
  66.                         </Setter.Value>
  67.                     </Setter>
  68.                 </Style>
  69.                 <Style TargetType="{x:Type TreeViewItem}">
  70.                     <Setter Property="Background" Value="Transparent"/>
  71.                     <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
  72.                     <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
  73.                     <Setter Property="Padding" Value="1,0,0,0"/>
  74.                     <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
  75.                     <Setter Property="FocusVisualStyle" Value="{StaticResource TreeViewItemFocusVisual}"/>
  76.                     <Setter Property="Template">
  77.                         <Setter.Value>
  78.                             <ControlTemplate TargetType="{x:Type TreeViewItem}">
  79.                                 <Grid>
  80.                                     <Grid.ColumnDefinitions>
  81.                                         <ColumnDefinition MinWidth="19" Width="Auto"/>
  82.                                         <ColumnDefinition Width="Auto"/>
  83.                                         <ColumnDefinition Width="*"/>
  84.                                     </Grid.ColumnDefinitions>
  85.                                     <Grid.RowDefinitions>
  86.                                         <RowDefinition Height="Auto"/>
  87.                                         <RowDefinition/>
  88.                                     </Grid.RowDefinitions>
  89.                                     <ToggleButton x:Name="Expander" ClickMode="Press" IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ExpandCollapseToggleStyle}"/>
  90.                                     <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="1" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
  91.                                         <ContentPresenter x:Name="PART_Header" ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
  92.                                     </Border>
  93.                                     <ItemsPresenter x:Name="ItemsHost" Grid.ColumnSpan="2" Grid.Column="1" Grid.Row="1"/>
  94.                                 </Grid>
  95.                                 <ControlTemplate.Triggers>
  96.                                     <Trigger Property="IsExpanded" Value="false">
  97.                                         <Setter Property="Visibility" TargetName="ItemsHost" Value="Collapsed"/>
  98.                                     </Trigger>
  99.                                     <Trigger Property="HasItems" Value="false">
  100.                                         <Setter Property="Visibility" TargetName="Expander" Value="Hidden"/>
  101.                                     </Trigger>
  102.                                     <Trigger Property="IsSelected" Value="true">
  103.                                         <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
  104.                                         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
  105.                                     </Trigger>
  106.                                     <MultiTrigger>
  107.                                         <MultiTrigger.Conditions>
  108.                                             <Condition Property="IsSelected" Value="true"/>
  109.                                             <Condition Property="IsSelectionActive" Value="false"/>
  110.                                         </MultiTrigger.Conditions>
  111.                                         <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
  112.                                         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}}"/>
  113.                                     </MultiTrigger>
  114.                                     <Trigger Property="IsEnabled" Value="false">
  115.                                         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
  116.                                     </Trigger>
  117.                                 </ControlTemplate.Triggers>
  118.                             </ControlTemplate>
  119.                         </Setter.Value>
  120.                     </Setter>
  121.                     <Style.Triggers>
  122.                         <Trigger Property="VirtualizingPanel.IsVirtualizing" Value="true">
  123.                             <Setter Property="ItemsPanel">
  124.                                 <Setter.Value>
  125.                                     <ItemsPanelTemplate>
  126.                                         <VirtualizingStackPanel/>
  127.                                     </ItemsPanelTemplate>
  128.                                 </Setter.Value>
  129.                             </Setter>
  130.                         </Trigger>
  131.                     </Style.Triggers>
  132.                 </Style>
  133.             </Style.Resources>
  134.            
  135.             <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
  136.             <Setter Property="BorderBrush" Value="{StaticResource ListBorder}"/>
  137.             <Setter Property="BorderThickness" Value="1"/>
  138.             <Setter Property="Padding" Value="1"/>
  139.             <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
  140.             <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
  141.             <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
  142.             <Setter Property="ScrollViewer.PanningMode" Value="Both"/>
  143.             <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
  144.             <Setter Property="VerticalContentAlignment" Value="Center"/>
  145.             <Setter Property="Template">
  146.                 <Setter.Value>
  147.                     <ControlTemplate TargetType="{x:Type TreeView}">
  148.                         <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="true">
  149.                             <ScrollViewer x:Name="_tv_scrollviewer_" Background="{TemplateBinding Background}" CanContentScroll="false" Focusable="false" HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}">
  150.                                 <ItemsPresenter/>
  151.                             </ScrollViewer>
  152.                         </Border>
  153.                         <ControlTemplate.Triggers>
  154.                             <Trigger Property="IsEnabled" Value="false">
  155.                                 <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
  156.                             </Trigger>
  157.                             <Trigger Property="VirtualizingPanel.IsVirtualizing" Value="true">
  158.                                 <Setter Property="CanContentScroll" TargetName="_tv_scrollviewer_" Value="true"/>
  159.                             </Trigger>
  160.                         </ControlTemplate.Triggers>
  161.                     </ControlTemplate>
  162.                 </Setter.Value>
  163.             </Setter>
  164.             <Style.Triggers>
  165.                 <Trigger Property="VirtualizingPanel.IsVirtualizing" Value="true">
  166.                     <Setter Property="ItemsPanel">
  167.                         <Setter.Value>
  168.                             <ItemsPanelTemplate>
  169.                                 <VirtualizingStackPanel/>
  170.                             </ItemsPanelTemplate>
  171.                         </Setter.Value>
  172.                     </Setter>
  173.                 </Trigger>
  174.             </Style.Triggers>
  175.         </Style>
  176.  
  177.     </Window.Resources>
  178.     <Grid>
  179.     <TreeView Style="{StaticResource TreeViewStyle1}">
  180.         <!--
  181.        <TreeView.Resources>
  182.            <Style TargetType="{x:Type TreeViewItem}">
  183.                <Setter Property="HeaderTemplate">
  184.                    <Setter.Value>
  185.                        <DataTemplate DataType="{x:Type TreeViewItem}">
  186.                            <StackPanel Orientation="Vertical">
  187.                                <TextBlock Text="{Binding}" />
  188.                                <TextBlock Text="{Binding}" Foreground="Red"/>
  189.                            </StackPanel>
  190.                        </DataTemplate>
  191.                    </Setter.Value>
  192.                </Setter>
  193.            </Style>
  194.        </TreeView.Resources>
  195.        -->
  196.         <TreeViewItem Header="Root">
  197.             <TreeViewItem Header="Sub" />
  198.         </TreeViewItem>
  199.     </TreeView>
  200.     </Grid>
  201. </Window>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement