<!-- Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License. See LICENSE in the project root for license information. -->
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:local="clr-namespace:ModernWpf.Controls"
    xmlns:primitives="clr-namespace:ModernWpf.Controls.Primitives">

    <sys:Double x:Key="TreeViewItemMinHeight">32</sys:Double>

    <Style x:Key="ExpandCollapseToggleStyle" TargetType="ToggleButton">
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="Focusable" Value="False" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ToggleButton">
                    <Border
                        Background="{TemplateBinding Background}"
                        Padding="{TemplateBinding Padding}">
                        <ContentPresenter />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="DefaultTreeViewItemStyle" TargetType="TreeViewItem">
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="FontFamily" Value="{DynamicResource ContentControlThemeFontFamily}" />
        <Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" />
        <Setter Property="Background" Value="{DynamicResource TreeViewItemBackground}" />
        <Setter Property="Foreground" Value="{DynamicResource ListViewItemForeground}" />
        <Setter Property="BorderBrush" Value="{DynamicResource TreeViewItemBorderBrush}" />
        <Setter Property="BorderThickness" Value="{DynamicResource TreeViewItemBorderThemeThickness}" />
        <Setter Property="KeyboardNavigation.TabNavigation" Value="Local" />
        <!--<Setter Property="IsHoldingEnabled" Value="True" />-->
        <Setter Property="Padding" Value="0" />
        <Setter Property="HorizontalContentAlignment" Value="Left" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="MinWidth" Value="{DynamicResource ListViewItemMinWidth}" />
        <Setter Property="MinHeight" Value="{DynamicResource TreeViewItemMinHeight}" />
        <Setter Property="FocusVisualStyle" Value="{DynamicResource {x:Static SystemParameters.FocusVisualStyleKey}}" />
        <Setter Property="primitives:FocusVisualHelper.UseSystemFocusVisuals" Value="{DynamicResource UseSystemFocusVisuals}" />
        <Setter Property="primitives:TreeViewItemHelper.IsEnabled" Value="True" />
        <Setter Property="primitives:TreeViewItemHelper.GlyphBrush" Value="{DynamicResource TreeViewItemForeground}" />
        <Setter Property="primitives:TreeViewItemHelper.CollapsedPath" Value="M 5.029297 19.091797 L 14.111328 10 L 5.029297 0.908203 L 5.908203 0.029297 L 15.888672 10 L 5.908203 19.970703 Z" />
        <Setter Property="primitives:TreeViewItemHelper.ExpandedPath" Value="M 18.935547 4.560547 L 19.814453 5.439453 L 10 15.253906 L 0.185547 5.439453 L 1.064453 4.560547 L 10 13.496094 Z" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TreeViewItem">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition />
                        </Grid.RowDefinitions>

                        <Border
                            x:Name="ContentPresenterGrid"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            CornerRadius="{TemplateBinding primitives:ControlHelper.CornerRadius}"
                            MinHeight="{TemplateBinding MinHeight}"
                            Padding="{TemplateBinding primitives:TreeViewItemHelper.Indentation}"
                            SnapsToDevicePixels="true"
                            primitives:PressHelper.IsEnabled="True">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition />
                                </Grid.ColumnDefinitions>

                                <ToggleButton
                                    x:Name="ExpandCollapseChevron"
                                    Grid.Column="0"
                                    Padding="12,0,12,0"
                                    Width="Auto"
                                    Opacity="{TemplateBinding primitives:TreeViewItemHelper.GlyphOpacity}"
                                    Background="Transparent"
                                    IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}"
                                    Style="{StaticResource ExpandCollapseToggleStyle}">
                                    <Grid>
                                        <local:FontIconFallback
                                            x:Name="CollapsedGlyph"
                                            Foreground="{TemplateBinding primitives:TreeViewItemHelper.GlyphBrush}"
                                            Width="12"
                                            Height="12"
                                            Visibility="Collapsed"
                                            FontSize="{TemplateBinding primitives:TreeViewItemHelper.GlyphSize}"
                                            Data="{TemplateBinding primitives:TreeViewItemHelper.CollapsedPath}"
                                            FontFamily="{DynamicResource SymbolThemeFontFamily}"
                                            VerticalAlignment="Center"
                                            IsHitTestVisible="False"/>
                                        <local:FontIconFallback
                                            x:Name="ExpandedGlyph"
                                            Foreground="{TemplateBinding primitives:TreeViewItemHelper.GlyphBrush}"
                                            Width="12"
                                            Height="12"
                                            Visibility="Collapsed"
                                            FontSize="{TemplateBinding primitives:TreeViewItemHelper.GlyphSize}"
                                            Data="{TemplateBinding primitives:TreeViewItemHelper.ExpandedPath}"
                                            FontFamily="{DynamicResource SymbolThemeFontFamily}"
                                            VerticalAlignment="Center"
                                            IsHitTestVisible="False"/>
                                    </Grid>
                                </ToggleButton>

                                <ContentPresenter
                                    x:Name="PART_Header"
                                    Grid.Column="1"
                                    ContentSource="Header"
                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                    Margin="{TemplateBinding Padding}"
                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                            </Grid>
                        </Border>

                        <ItemsPresenter
                            x:Name="ItemsHost"
                            Grid.Row="1" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsExpanded" Value="False">
                            <Setter TargetName="CollapsedGlyph" Property="Visibility" Value="Visible" />
                            <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
                        </Trigger>
                        <Trigger Property="IsExpanded" Value="True">
                            <Setter TargetName="ExpandedGlyph" Property="Visibility" Value="Visible" />
                        </Trigger>
                        <Trigger Property="HasItems" Value="False">
                            <Setter TargetName="ExpandCollapseChevron" Property="Visibility" Value="Hidden" />
                            <Setter Property="primitives:TreeViewItemHelper.GlyphOpacity" Value="0" />
                        </Trigger>
                        <Trigger SourceName="ContentPresenterGrid" Property="IsMouseOver" Value="True">
                            <Setter TargetName="ContentPresenterGrid" Property="Background" Value="{DynamicResource TreeViewItemBackgroundPointerOver}" />
                            <Setter TargetName="PART_Header" Property="TextElement.Foreground" Value="{DynamicResource TreeViewItemForegroundPointerOver}" />
                            <Setter TargetName="ContentPresenterGrid" Property="BorderBrush" Value="{DynamicResource TreeViewItemBorderBrushPointerOver}" />
                        </Trigger>
                        <Trigger SourceName="ContentPresenterGrid" Property="primitives:PressHelper.IsPressed" Value="True">
                            <Setter TargetName="ContentPresenterGrid" Property="Background" Value="{DynamicResource TreeViewItemBackgroundPressed}" />
                            <Setter TargetName="PART_Header" Property="TextElement.Foreground" Value="{DynamicResource TreeViewItemForegroundPressed}" />
                            <Setter TargetName="ContentPresenterGrid" Property="BorderBrush" Value="{DynamicResource TreeViewItemBorderBrushPressed}" />
                        </Trigger>
                        <!-- Selected -->
                        <Trigger Property="IsSelected" Value="True">
                            <Setter TargetName="ContentPresenterGrid" Property="Background" Value="{DynamicResource TreeViewItemBackgroundSelected}" />
                            <Setter TargetName="PART_Header" Property="TextElement.Foreground" Value="{DynamicResource TreeViewItemForegroundSelected}" />
                            <Setter TargetName="ContentPresenterGrid" Property="BorderBrush" Value="{DynamicResource TreeViewItemBorderBrushSelected}" />
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter TargetName="ContentPresenterGrid" Property="Background" Value="{DynamicResource TreeViewItemBackgroundDisabled}" />
                            <Setter TargetName="PART_Header" Property="TextElement.Foreground" Value="{DynamicResource TreeViewItemForegroundDisabled}" />
                            <Setter TargetName="ContentPresenterGrid" Property="BorderBrush" Value="{DynamicResource TreeViewItemBorderBrushDisabled}" />
                        </Trigger>
                        <!-- PointerOverSelected -->
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition SourceName="ContentPresenterGrid" Property="IsMouseOver" Value="True" />
                                <Condition Property="IsSelected" Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter TargetName="ContentPresenterGrid" Property="Background" Value="{DynamicResource TreeViewItemBackgroundSelectedPointerOver}" />
                            <Setter TargetName="PART_Header" Property="TextElement.Foreground" Value="{DynamicResource TreeViewItemForegroundSelectedPointerOver}" />
                            <Setter TargetName="ContentPresenterGrid" Property="BorderBrush" Value="{DynamicResource TreeViewItemBorderBrushSelectedPointerOver}" />
                        </MultiTrigger>
                        <!-- PressedSelected -->
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition SourceName="ContentPresenterGrid" Property="primitives:PressHelper.IsPressed" Value="True" />
                                <Condition Property="IsSelected" Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter TargetName="ContentPresenterGrid" Property="Background" Value="{DynamicResource TreeViewItemBackgroundSelectedPressed}" />
                            <Setter TargetName="PART_Header" Property="TextElement.Foreground" Value="{DynamicResource TreeViewItemForegroundSelectedPressed}" />
                            <Setter TargetName="ContentPresenterGrid" Property="BorderBrush" Value="{DynamicResource TreeViewItemBorderBrushSelectedPressed}" />
                        </MultiTrigger>
                        <!-- SelectedDisabled -->
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="True" />
                                <Condition Property="IsEnabled" Value="False" />
                            </MultiTrigger.Conditions>
                            <Setter TargetName="ContentPresenterGrid" Property="Background" Value="{DynamicResource TreeViewItemBackgroundSelectedDisabled}" />
                            <Setter TargetName="PART_Header" Property="TextElement.Foreground" Value="{DynamicResource TreeViewItemForegroundSelectedDisabled}" />
                            <Setter TargetName="ContentPresenterGrid" Property="BorderBrush" Value="{DynamicResource TreeViewItemBorderBrushSelectedDisabled}" />
                        </MultiTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="VirtualizingPanel.IsVirtualizing" Value="true">
                <Setter Property="ItemsPanel">
                    <Setter.Value>
                        <ItemsPanelTemplate>
                            <VirtualizingStackPanel />
                        </ItemsPanelTemplate>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>

    <Style TargetType="TreeViewItem" BasedOn="{StaticResource DefaultTreeViewItemStyle}" />

    <Style x:Key="DefaultTreeViewStyle" TargetType="TreeView">
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
        <Setter Property="ScrollViewer.PanningMode" Value="Both" />
        <Setter Property="Stylus.IsFlicksEnabled" Value="False" />
        <Setter Property="FocusVisualStyle" Value="{DynamicResource {x:Static SystemParameters.FocusVisualStyleKey}}" />
        <Setter Property="primitives:FocusVisualHelper.UseSystemFocusVisuals" Value="{DynamicResource UseSystemFocusVisuals}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TreeView">
                    <Border
                        x:Name="Bd"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        SnapsToDevicePixels="true">
                        <ScrollViewer
                            x:Name="_tv_scrollviewer_"
                            Padding="{TemplateBinding Padding}"
                            Background="{TemplateBinding Background}"
                            CanContentScroll="false"
                            Focusable="false"
                            HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                            VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}">
                            <ItemsPresenter />
                        </ScrollViewer>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="VirtualizingPanel.IsVirtualizing" Value="true">
                            <Setter TargetName="_tv_scrollviewer_" Property="CanContentScroll" Value="true" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="VirtualizingPanel.IsVirtualizing" Value="true">
                <Setter Property="ItemsPanel">
                    <Setter.Value>
                        <ItemsPanelTemplate>
                            <VirtualizingStackPanel />
                        </ItemsPanelTemplate>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>

    <Style TargetType="TreeView" BasedOn="{StaticResource DefaultTreeViewStyle}" />

</ResourceDictionary>