Browse Source

TreeViewItem updates.

pull/1831/head
William David Cossey 8 years ago
parent
commit
f6dfd1688e
  1. 29
      src/Avalonia.Controls/TreeViewItem.cs
  2. 15
      src/Avalonia.Styling/LogicalTree/LogicalExtensions.cs
  3. 163
      src/Avalonia.Themes.Default/TreeViewItem.xaml

29
src/Avalonia.Controls/TreeViewItem.cs

@ -6,7 +6,6 @@ using Avalonia.Controls.Generators;
using Avalonia.Controls.Mixins;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.LogicalTree;
@ -37,8 +36,7 @@ namespace Avalonia.Controls
/// </summary>
public static readonly DirectProperty<TreeViewItem, int> DepthProperty =
AvaloniaProperty.RegisterDirect<TreeViewItem, int>(
nameof(Depth),
o => o.Depth);
nameof(Depth), o => o.Depth);
private static readonly ITemplate<IPanel> DefaultPanel =
new FuncTemplate<IPanel>(() => new StackPanel());
@ -80,27 +78,8 @@ namespace Avalonia.Controls
/// </summary>
public int Depth
{
get { return this.GetDepth(this); }
}
private int GetDepth(TreeViewItem item)
{
TreeViewItem parent;
while ((parent = GetParent(item)) != null)
{
return GetDepth(parent) + 1;
}
return 0;
}
private static TreeViewItem GetParent(TreeViewItem item)
{
var parent = item.InheritanceParent;
while (!(parent == null || parent is TreeViewItem || parent is TreeView))
{
parent = item.Parent;
}
return parent as TreeViewItem;
get { return _depth; }
private set { SetAndRaise(DepthProperty, ref _depth, value); }
}
/// <summary>
@ -127,6 +106,8 @@ namespace Avalonia.Controls
base.OnAttachedToLogicalTree(e);
_treeView = this.GetLogicalAncestors().OfType<TreeView>().FirstOrDefault();
Depth = this.CalculateDistanceFromLogicalParent<TreeView>() - 1;
if (ItemTemplate == null && _treeView?.ItemTemplate != null)
{
ItemTemplate = _treeView.ItemTemplate;

15
src/Avalonia.Styling/LogicalTree/LogicalExtensions.cs

@ -87,5 +87,20 @@ namespace Avalonia.LogicalTree
{
return target.GetLogicalAncestors().Any(x => x == logical);
}
public static int CalculateDistanceFromLogicalParent<T>(this ILogical logical, int @default = -1) where T : class
{
Contract.Requires<ArgumentNullException>(logical != null);
var result = 0;
while (logical != null && logical.GetType() != typeof(T))
{
++result;
logical = logical.LogicalParent;
}
return logical != null ? result : @default;
}
}
}

163
src/Avalonia.Themes.Default/TreeViewItem.xaml

@ -1,91 +1,92 @@
<Styles xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Avalonia.Controls.Converters;assembly=Avalonia.Controls">
<Style Selector="TreeViewItem">
<Style.Resources>
<local:MarginMultiplierConverter Indent="16" Left="True" x:Key="leftMarginConverter" />
</Style.Resources>
<Setter Property="Padding" Value="2"/>
<Setter Property="Background" Value="{DynamicResource InvisibleSelectionBrush}"/>
<Setter Property="Template">
<ControlTemplate>
<StackPanel>
<Border Name="SelectionBorder"
Focusable="True"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
TemplatedControl.IsTemplateFocusTarget="True">
<Grid ColumnDefinitions="16, *" Margin="{TemplateBinding Depth, Converter={StaticResource leftMarginConverter}}" >
<ToggleButton Name="expander"
Focusable="False"
IsChecked="{TemplateBinding IsExpanded, Mode=TwoWay}"/>
<ContentPresenter Name="PART_HeaderPresenter"
Focusable="False"
Content="{TemplateBinding Header}"
HorizontalContentAlignment="{TemplateBinding HorizontalAlignment}"
Padding="{TemplateBinding Padding}"
Grid.Column="1"/>
</Grid>
</Border>
<ItemsPresenter Name="PART_ItemsPresenter"
IsVisible="{TemplateBinding IsExpanded}"
Items="{TemplateBinding Items}"
ItemsPanel="{TemplateBinding ItemsPanel}"
MemberSelector="{TemplateBinding MemberSelector}"/>
</StackPanel>
</ControlTemplate>
</Setter>
</Style>
xmlns:converters="clr-namespace:Avalonia.Controls.Converters;assembly=Avalonia.Controls">
<Style Selector="TreeViewItem">
<Style.Resources>
<converters:MarginMultiplierConverter Indent="16" Left="True" x:Key="LeftMarginConverter" />
</Style.Resources>
<Setter Property="Padding" Value="2"/>
<Setter Property="Background" Value="{DynamicResource InvisibleSelectionBrush}"/>
<Setter Property="Template">
<ControlTemplate>
<StackPanel>
<Border Name="SelectionBorder"
Focusable="True"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
TemplatedControl.IsTemplateFocusTarget="True">
<Grid ColumnDefinitions="16, *"
Margin="{TemplateBinding Depth, Mode=OneWay, Converter={StaticResource LeftMarginConverter}}" >
<ToggleButton Name="expander"
Focusable="False"
IsChecked="{TemplateBinding IsExpanded, Mode=TwoWay}"/>
<ContentPresenter Name="PART_HeaderPresenter"
Focusable="False"
Content="{TemplateBinding Header}"
HorizontalContentAlignment="{TemplateBinding HorizontalAlignment}"
Padding="{TemplateBinding Padding}"
Grid.Column="1"/>
</Grid>
</Border>
<ItemsPresenter Name="PART_ItemsPresenter"
IsVisible="{TemplateBinding IsExpanded}"
Items="{TemplateBinding Items}"
ItemsPanel="{TemplateBinding ItemsPanel}"
MemberSelector="{TemplateBinding MemberSelector}"/>
</StackPanel>
</ControlTemplate>
</Setter>
</Style>
<Style Selector="TreeViewItem /template/ ToggleButton#expander">
<Setter Property="Template">
<ControlTemplate>
<Border Background="Transparent"
Width="14"
Height="12"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Path Fill="{DynamicResource ThemeForegroundBrush}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Data="M 0 2 L 4 6 L 0 10 Z"/>
</Border>
</ControlTemplate>
</Setter>
</Style>
<Style Selector="TreeViewItem /template/ ToggleButton#expander">
<Setter Property="Template">
<ControlTemplate>
<Border Background="Transparent"
Width="14"
Height="12"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Path Fill="{DynamicResource ThemeForegroundBrush}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Data="M 0 2 L 4 6 L 0 10 Z"/>
</Border>
</ControlTemplate>
</Setter>
</Style>
<Style Selector="TreeViewItem /template/ ContentPresenter#PART_HeaderPresenter">
<Setter Property="Padding" Value="2"/>
</Style>
<Style Selector="TreeViewItem[IsSelected=false] /template/ Border#SelectionBorder[IsPointerOver=true]">
<Setter Property="Background" Value="{DynamicResource ThemeControlHighlightMidBrush}"/>
</Style>
<Style Selector="TreeViewItem /template/ ContentPresenter#PART_HeaderPresenter">
<Setter Property="Padding" Value="2"/>
</Style>
<Style Selector="TreeViewItem[IsSelected=true] /template/ Border#SelectionBorder">
<Setter Property="Background" Value="{DynamicResource ThemeAccentBrush4}"/>
</Style>
<Style Selector="TreeViewItem[IsSelected=false] /template/ Border#SelectionBorder[IsPointerOver=true]">
<Setter Property="Background" Value="{DynamicResource ThemeControlHighlightMidBrush}"/>
</Style>
<Style Selector="TreeViewItem[IsSelected=true] /template/ Border#SelectionBorder[IsPointerOver=false][IsFocused=true]">
<Setter Property="Background" Value="{DynamicResource ThemeAccentBrush3}"/>
</Style>
<Style Selector="TreeViewItem[IsSelected=true] /template/ Border#SelectionBorder">
<Setter Property="Background" Value="{DynamicResource ThemeAccentBrush4}"/>
</Style>
<Style Selector="TreeViewItem[IsSelected=true] /template/ Border#SelectionBorder[IsPointerOver=true][IsFocused=false]">
<Setter Property="Background" Value="{DynamicResource ThemeAccentBrush3}"/>
</Style>
<Style Selector="TreeViewItem[IsSelected=true] /template/ Border#SelectionBorder[IsPointerOver=false][IsFocused=true]">
<Setter Property="Background" Value="{DynamicResource ThemeAccentBrush3}"/>
</Style>
<Style Selector="TreeViewItem[IsSelected=true] /template/ Border#SelectionBorder[IsPointerOver=true][IsFocused=true]">
<Setter Property="Background" Value="{DynamicResource ThemeAccentBrush2}"/>
</Style>
<Style Selector="TreeViewItem /template/ ToggleButton#expander:checked">
<Setter Property="RenderTransform">
<RotateTransform Angle="45"/>
</Setter>
</Style>
<Style Selector="TreeViewItem[IsSelected=true] /template/ Border#SelectionBorder[IsPointerOver=true][IsFocused=false]">
<Setter Property="Background" Value="{DynamicResource ThemeAccentBrush3}"/>
</Style>
<Style Selector="TreeViewItem:empty /template/ ToggleButton#expander">
<Setter Property="IsVisible" Value="False"/>
</Style>
<Style Selector="TreeViewItem[IsSelected=true] /template/ Border#SelectionBorder[IsPointerOver=true][IsFocused=true]">
<Setter Property="Background" Value="{DynamicResource ThemeAccentBrush2}"/>
</Style>
<Style Selector="TreeViewItem /template/ ToggleButton#expander:checked">
<Setter Property="RenderTransform">
<RotateTransform Angle="45"/>
</Setter>
</Style>
<Style Selector="TreeViewItem:empty /template/ ToggleButton#expander">
<Setter Property="IsVisible" Value="False"/>
</Style>
</Styles>

Loading…
Cancel
Save