优化更新代码,添加界面功能并整合
This commit is contained in:
101
WPFluent/Controls/TreeListView/TreeListView.cs
Normal file
101
WPFluent/Controls/TreeListView/TreeListView.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
|
||||
using WPFluent.Controls.Primitives;
|
||||
|
||||
namespace WPFluent.Controls;
|
||||
|
||||
public class TreeListView : TreeView
|
||||
{
|
||||
public new object SelectedItem
|
||||
{
|
||||
get => GetValue(SelectedItemProperty);
|
||||
set => SetValue(SelectedItemProperty, value);
|
||||
}
|
||||
|
||||
public new static readonly DependencyProperty SelectedItemProperty =
|
||||
DependencyProperty.Register(nameof(SelectedItem), typeof(object), typeof(TreeListView), new PropertyMetadata(null, OnNewSelectedItemChanged));
|
||||
|
||||
private static void OnNewSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
// Use the private `TreeView::SelectedItemProperty` to
|
||||
// allow writing to the readonly `TreeView::SelectedItemProperty`.
|
||||
if (d is TreeListView treeListView)
|
||||
{
|
||||
object? newValue = e.NewValue;
|
||||
|
||||
if (typeof(TreeView).GetField("SelectedItemPropertyKey", BindingFlags.NonPublic | BindingFlags.Static) is FieldInfo fieldInfo)
|
||||
{
|
||||
DependencyPropertyKey selectedItemPropertyKey = (DependencyPropertyKey)fieldInfo.GetValue(null)!;
|
||||
object? currentValue = treeListView.GetValue(TreeView.SelectedItemProperty);
|
||||
|
||||
if (currentValue != newValue)
|
||||
{
|
||||
treeListView.SetValue(selectedItemPropertyKey, newValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public TreeListView()
|
||||
{
|
||||
SelectedItemChanged += (sender, e) =>
|
||||
{
|
||||
// Avoid exceptions caused by possible inconsistencies with
|
||||
// the handling of readonly `TreeView::SelectedItemProperty`.
|
||||
try
|
||||
{
|
||||
if (SelectedItem != base.SelectedItem)
|
||||
{
|
||||
SelectedItem = base.SelectedItem;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine(ex);
|
||||
}
|
||||
};
|
||||
|
||||
MouseRightButtonDown += (_, e) =>
|
||||
{
|
||||
if (e.OriginalSource is DependencyObject source)
|
||||
{
|
||||
if (source.FindAscendant<TreeViewItem>() is TreeViewItem { } treeViewItem)
|
||||
{
|
||||
treeViewItem.IsSelected = true;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public GridViewColumnCollection Columns
|
||||
{
|
||||
get => (GridViewColumnCollection)GetValue(ColumnsProperty);
|
||||
set => SetValue(ColumnsProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty ColumnsProperty =
|
||||
DependencyProperty.Register(nameof(Columns), typeof(GridViewColumnCollection), typeof(TreeListView), new PropertyMetadata(null));
|
||||
|
||||
public CornerRadius CornerRadius
|
||||
{
|
||||
get => (CornerRadius)GetValue(CornerRadiusProperty);
|
||||
set => SetValue(CornerRadiusProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty CornerRadiusProperty =
|
||||
DependencyProperty.Register(nameof(CornerRadius), typeof(CornerRadius), typeof(TreeListView), new PropertyMetadata(new CornerRadius(4d)));
|
||||
|
||||
protected override DependencyObject GetContainerForItemOverride()
|
||||
{
|
||||
return new TreeListViewItem();
|
||||
}
|
||||
|
||||
protected override bool IsItemItsOwnContainerOverride(object item)
|
||||
{
|
||||
return item is TreeListViewItem;
|
||||
}
|
||||
}
|
||||
256
WPFluent/Controls/TreeListView/TreeListView.xaml
Normal file
256
WPFluent/Controls/TreeListView/TreeListView.xaml
Normal file
@@ -0,0 +1,256 @@
|
||||
<ResourceDictionary
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:conv="clr-namespace:WPFluent.Converters"
|
||||
xmlns:layout="clr-namespace:WPFluent.Layout"
|
||||
xmlns:controls="clr-namespace:WPFluent.Controls">
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
<ResourceDictionary Source="pack://application:,,,/WPFluent;component/Controls/TreeView/TreeViewItem.xaml" />
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
<conv:TreeLevelToIndentConverter x:Key="TreeLevelToIndentConverter" />
|
||||
<FontFamily x:Key="SymbolThemeFontFamily">pack://application:,,,/WPFluent;component/Resources/Fonts/Segoe Fluent Icons.ttf#Segoe Fluent Icons</FontFamily>
|
||||
|
||||
<Style x:Key="TreeExpandCollapseToggleStyle" TargetType="{x:Type ToggleButton}">
|
||||
<Setter Property="FocusVisualStyle" Value="{DynamicResource DefaultControlFocusVisualStyle}" />
|
||||
<Setter Property="Focusable" Value="False" />
|
||||
<Setter Property="OverridesDefaultStyle" Value="True" />
|
||||
<Setter Property="SnapsToDevicePixels" Value="True" />
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="{x:Type ToggleButton}">
|
||||
<Grid
|
||||
x:Name="ChevronContainer"
|
||||
Width="15"
|
||||
Height="15"
|
||||
Background="Transparent"
|
||||
RenderTransformOrigin="0.5, 0.5">
|
||||
<Grid.RenderTransform>
|
||||
<RotateTransform Angle="0" />
|
||||
</Grid.RenderTransform>
|
||||
<controls:SymbolIcon
|
||||
x:Name="ChevronIcon"
|
||||
VerticalAlignment="Center"
|
||||
FontSize="{StaticResource TreeViewItemChevronSize}"
|
||||
Symbol="ChevronRight28" />
|
||||
</Grid>
|
||||
<ControlTemplate.Triggers>
|
||||
<Trigger Property="IsChecked" Value="True">
|
||||
<Trigger.EnterActions>
|
||||
<BeginStoryboard>
|
||||
<Storyboard>
|
||||
<DoubleAnimation
|
||||
Storyboard.TargetName="ChevronContainer"
|
||||
Storyboard.TargetProperty="(Grid.RenderTransform).(RotateTransform.Angle)"
|
||||
To="90"
|
||||
Duration="0:0:0.16" />
|
||||
</Storyboard>
|
||||
</BeginStoryboard>
|
||||
</Trigger.EnterActions>
|
||||
<Trigger.ExitActions>
|
||||
<BeginStoryboard>
|
||||
<Storyboard>
|
||||
<DoubleAnimation
|
||||
Storyboard.TargetName="ChevronContainer"
|
||||
Storyboard.TargetProperty="(Grid.RenderTransform).(RotateTransform.Angle)"
|
||||
To="0"
|
||||
Duration="0:0:0.16" />
|
||||
</Storyboard>
|
||||
</BeginStoryboard>
|
||||
</Trigger.ExitActions>
|
||||
</Trigger>
|
||||
</ControlTemplate.Triggers>
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
</Style>
|
||||
|
||||
<Style x:Key="DefaultTreeRowExpanderStyle" TargetType="{x:Type controls:TreeRowExpander}">
|
||||
<Setter Property="Foreground" Value="{DynamicResource TextFillColorPrimaryBrush}" />
|
||||
<Setter Property="Focusable" Value="False" />
|
||||
<Setter Property="Background" Value="Transparent" />
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="{x:Type controls:TreeRowExpander}">
|
||||
<layout:Grid VerticalAlignment="Center" ColumnDefinitions="Auto,*">
|
||||
<ToggleButton
|
||||
x:Name="Expander"
|
||||
Grid.Column="0"
|
||||
Margin="{Binding Path=Level, Converter={StaticResource TreeLevelToIndentConverter}, RelativeSource={RelativeSource AncestorType={x:Type controls:TreeListViewItem}}}"
|
||||
ClickMode="Press"
|
||||
IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource AncestorType={x:Type controls:TreeListViewItem}}}"
|
||||
Style="{StaticResource TreeExpandCollapseToggleStyle}" />
|
||||
<ContentPresenter
|
||||
Grid.Column="1"
|
||||
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
|
||||
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
|
||||
Content="{TemplateBinding Content}"
|
||||
TextElement.FontSize="{TemplateBinding FontSize}"
|
||||
TextElement.Foreground="{TemplateBinding Foreground}" />
|
||||
</layout:Grid>
|
||||
<ControlTemplate.Triggers>
|
||||
<DataTrigger Binding="{Binding Path=HasItems, RelativeSource={RelativeSource AncestorType={x:Type controls:TreeListViewItem}}}" Value="false">
|
||||
<Setter TargetName="Expander" Property="Visibility" Value="Hidden" />
|
||||
</DataTrigger>
|
||||
<DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type controls:TreeListViewItem}}}" Value="true">
|
||||
<Setter Property="Foreground" Value="{DynamicResource ButtonForeground}" />
|
||||
</DataTrigger>
|
||||
</ControlTemplate.Triggers>
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
</Style>
|
||||
|
||||
<Style BasedOn="{StaticResource DefaultTreeRowExpanderStyle}" TargetType="{x:Type controls:TreeRowExpander}" />
|
||||
|
||||
<Style x:Key="DefaultTreeListViewItemStyle" TargetType="{x:Type controls:TreeListViewItem}">
|
||||
<Setter Property="FocusVisualStyle" Value="{DynamicResource DefaultControlFocusVisualStyle}" />
|
||||
<Setter Property="Foreground" Value="{DynamicResource TreeViewItemForeground}" />
|
||||
<Setter Property="Background" Value="{DynamicResource TreeViewItemBackground}" />
|
||||
<Setter Property="Padding" Value="4" />
|
||||
<Setter Property="FontSize" Value="{StaticResource TreeViewItemFontSize}" />
|
||||
<Setter Property="Border.CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
|
||||
<Setter Property="IsTabStop" Value="True" />
|
||||
<Setter Property="OverridesDefaultStyle" Value="True" />
|
||||
<Setter Property="SnapsToDevicePixels" Value="True" />
|
||||
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
|
||||
<Setter Property="VerticalContentAlignment" Value="Center" />
|
||||
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
|
||||
<Setter Property="IsExpanded" Value="{Binding IsExpanded}" />
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="{x:Type controls:TreeListViewItem}">
|
||||
<layout:Grid VerticalAlignment="Center" RowDefinitions="Auto,*">
|
||||
<Border
|
||||
x:Name="PART_OuterBorder"
|
||||
Grid.Row="0"
|
||||
MinHeight="30"
|
||||
Margin="0"
|
||||
Padding="0"
|
||||
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
|
||||
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
|
||||
CornerRadius="4"
|
||||
SnapsToDevicePixels="True">
|
||||
<Border
|
||||
x:Name="PART_InnerBorder"
|
||||
Padding="{TemplateBinding Padding}"
|
||||
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
|
||||
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
|
||||
Background="{TemplateBinding Background}"
|
||||
CornerRadius="4"
|
||||
SnapsToDevicePixels="True">
|
||||
<controls:GridViewRowPresenter
|
||||
x:Name="PART_Header"
|
||||
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
|
||||
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
|
||||
Columns="{Binding Path=Columns, RelativeSource={RelativeSource AncestorType={x:Type controls:TreeListView}}}"
|
||||
Content="{TemplateBinding Header}"
|
||||
TextElement.FontSize="{TemplateBinding FontSize}" />
|
||||
</Border>
|
||||
</Border>
|
||||
<ItemsPresenter
|
||||
x:Name="ItemsHost"
|
||||
Grid.Row="1"
|
||||
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
|
||||
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
|
||||
</layout:Grid>
|
||||
<ControlTemplate.Triggers>
|
||||
<Trigger SourceName="PART_InnerBorder" Property="IsMouseOver" Value="true">
|
||||
<Setter Property="Foreground" Value="{DynamicResource ButtonForegroundPointerOver}" />
|
||||
<Setter TargetName="PART_InnerBorder" Property="Background" Value="{DynamicResource ListViewItemBackgroundPointerOver}" />
|
||||
</Trigger>
|
||||
<Trigger Property="IsSelected" Value="true">
|
||||
<Setter Property="Foreground" Value="{DynamicResource ButtonForeground}" />
|
||||
<Setter TargetName="PART_InnerBorder" Property="Background" Value="{DynamicResource ButtonBackgroundPointerOver}" />
|
||||
</Trigger>
|
||||
<Trigger Property="IsExpanded" Value="false">
|
||||
<Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
|
||||
</Trigger>
|
||||
<Trigger Property="IsEnabled" Value="false">
|
||||
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
|
||||
</Trigger>
|
||||
<MultiTrigger>
|
||||
<MultiTrigger.Conditions>
|
||||
<Condition Property="HasHeader" Value="false" />
|
||||
<Condition Property="Width" Value="Auto" />
|
||||
</MultiTrigger.Conditions>
|
||||
<Setter TargetName="PART_Header" Property="MinWidth" Value="75" />
|
||||
</MultiTrigger>
|
||||
<MultiTrigger>
|
||||
<MultiTrigger.Conditions>
|
||||
<Condition Property="HasHeader" Value="false" />
|
||||
<Condition Property="Height" Value="Auto" />
|
||||
</MultiTrigger.Conditions>
|
||||
<Setter TargetName="PART_Header" Property="MinHeight" Value="19" />
|
||||
</MultiTrigger>
|
||||
</ControlTemplate.Triggers>
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
</Style>
|
||||
|
||||
<Style BasedOn="{StaticResource DefaultTreeListViewItemStyle}" TargetType="{x:Type controls:TreeListViewItem}" />
|
||||
|
||||
<Style x:Key="DefaultTreeListViewStyle" TargetType="{x:Type controls:TreeListView}">
|
||||
<Setter Property="FocusVisualStyle" Value="{StaticResource DefaultControlFocusVisualStyle}" />
|
||||
<Setter Property="Foreground" Value="{DynamicResource TreeViewItemForeground}" />
|
||||
<Setter Property="Background" Value="Transparent" />
|
||||
<Setter Property="Margin" Value="0" />
|
||||
<Setter Property="Padding" Value="0" />
|
||||
<!-- Default BorderBrush is from WPF instead of WPF-UI -->
|
||||
<Setter Property="BorderBrush" Value="#828790" />
|
||||
<Setter Property="BorderThickness" Value="1" />
|
||||
<Setter Property="CornerRadius" Value="4" />
|
||||
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
|
||||
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
|
||||
<Setter Property="ScrollViewer.CanContentScroll" Value="False" />
|
||||
<Setter Property="VirtualizingPanel.IsVirtualizing" Value="True" />
|
||||
<Setter Property="OverridesDefaultStyle" Value="True" />
|
||||
<Setter Property="SnapsToDevicePixels" Value="True" />
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="{x:Type controls:TreeListView}">
|
||||
<Border
|
||||
x:Name="Bd"
|
||||
Padding="0"
|
||||
Background="{TemplateBinding Background}"
|
||||
BorderBrush="{TemplateBinding BorderBrush}"
|
||||
BorderThickness="{TemplateBinding BorderThickness}"
|
||||
CornerRadius="{TemplateBinding CornerRadius}">
|
||||
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled">
|
||||
<DockPanel>
|
||||
<controls:GridViewHeaderRowPresenter
|
||||
Margin="4"
|
||||
ColumnHeaderContainerStyle="{DynamicResource UiGridViewColumnHeaderStyle}"
|
||||
Columns="{TemplateBinding Columns}"
|
||||
DockPanel.Dock="Top"
|
||||
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
|
||||
<ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
|
||||
<ItemsPresenter />
|
||||
</ScrollViewer>
|
||||
</DockPanel>
|
||||
</ScrollViewer>
|
||||
</Border>
|
||||
<ControlTemplate.Triggers>
|
||||
<Trigger Property="IsEnabled" Value="False">
|
||||
<Setter TargetName="Bd" Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
|
||||
</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 BasedOn="{StaticResource DefaultTreeListViewStyle}" TargetType="{x:Type controls:TreeListView}" />
|
||||
|
||||
</ResourceDictionary>
|
||||
33
WPFluent/Controls/TreeListView/TreeListViewItem.cs
Normal file
33
WPFluent/Controls/TreeListView/TreeListViewItem.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System.Windows;
|
||||
|
||||
namespace WPFluent.Controls;
|
||||
|
||||
public class TreeListViewItem : TreeViewItem
|
||||
{
|
||||
private int _level = -1;
|
||||
|
||||
/// <summary>
|
||||
/// Item's hierarchy in the tree
|
||||
/// </summary>
|
||||
public int Level
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_level == -1)
|
||||
{
|
||||
_level = (ItemsControlFromItemContainer(this) is TreeListViewItem parent) ? parent.Level + 1 : 0;
|
||||
}
|
||||
return _level;
|
||||
}
|
||||
}
|
||||
|
||||
protected override DependencyObject GetContainerForItemOverride()
|
||||
{
|
||||
return new TreeListViewItem();
|
||||
}
|
||||
|
||||
protected override bool IsItemItsOwnContainerOverride(object item)
|
||||
{
|
||||
return item is TreeListViewItem;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user