Kategorie
This commit is contained in:
@@ -0,0 +1,256 @@
|
||||
using FasdDesktopUi.Basics.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Threading;
|
||||
|
||||
namespace FasdDesktopUi.Basics.UserControls
|
||||
{
|
||||
public partial class HierarchicalSelectionControl : UserControl, IFocusInvoker
|
||||
{
|
||||
private readonly ObservableCollection<HierarchicalSelectionItem> visibleItems = new ObservableCollection<HierarchicalSelectionItem>();
|
||||
private readonly Dictionary<string, HierarchicalSelectionItem> itemLookup = new Dictionary<string, HierarchicalSelectionItem>();
|
||||
private readonly DispatcherTimer searchDelayTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(250) };
|
||||
private string lastSearchText = string.Empty;
|
||||
public ObservableCollection<HierarchicalSelectionItem> VisibleItems => visibleItems;
|
||||
|
||||
public int? ParentIndex { get; set; }
|
||||
public UIElement ParentElement { get; set; }
|
||||
|
||||
public HierarchicalSelectionControl()
|
||||
{
|
||||
InitializeComponent();
|
||||
TreeViewControl.ItemsSource = visibleItems;
|
||||
searchDelayTimer.Tick += SearchDelayTimer_Tick;
|
||||
}
|
||||
|
||||
#region DependencyProperties
|
||||
|
||||
public ObservableCollection<HierarchicalSelectionItem> ItemsSource
|
||||
{
|
||||
get => (ObservableCollection<HierarchicalSelectionItem>)GetValue(ItemsSourceProperty);
|
||||
set => SetValue(ItemsSourceProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register(
|
||||
nameof(ItemsSource),
|
||||
typeof(ObservableCollection<HierarchicalSelectionItem>),
|
||||
typeof(HierarchicalSelectionControl),
|
||||
new PropertyMetadata(null, OnItemsSourceChanged));
|
||||
|
||||
private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
if (d is HierarchicalSelectionControl control)
|
||||
{
|
||||
control.RebuildLookup();
|
||||
control.ApplyFilter(control.lastSearchText);
|
||||
control.TryExpandToSelectedItem();
|
||||
}
|
||||
}
|
||||
|
||||
public HierarchicalSelectionItem SelectedItem
|
||||
{
|
||||
get => (HierarchicalSelectionItem)GetValue(SelectedItemProperty);
|
||||
set => SetValue(SelectedItemProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register(
|
||||
nameof(SelectedItem),
|
||||
typeof(HierarchicalSelectionItem),
|
||||
typeof(HierarchicalSelectionControl),
|
||||
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSelectedItemChanged));
|
||||
|
||||
private static void OnSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
if (d is HierarchicalSelectionControl control)
|
||||
{
|
||||
control.TryExpandToSelectedItem();
|
||||
}
|
||||
}
|
||||
|
||||
public Brush ComboBoxBackground
|
||||
{
|
||||
get => (Brush)GetValue(ComboBoxBackgroundProperty);
|
||||
set => SetValue(ComboBoxBackgroundProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty ComboBoxBackgroundProperty =
|
||||
DependencyProperty.Register(nameof(ComboBoxBackground), typeof(Brush), typeof(HierarchicalSelectionControl), new PropertyMetadata(Brushes.Transparent));
|
||||
|
||||
public string SearchPlaceholderText
|
||||
{
|
||||
get => (string)GetValue(SearchPlaceholderTextProperty);
|
||||
set => SetValue(SearchPlaceholderTextProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty SearchPlaceholderTextProperty =
|
||||
DependencyProperty.Register(nameof(SearchPlaceholderText), typeof(string), typeof(HierarchicalSelectionControl), new PropertyMetadata(string.Empty));
|
||||
|
||||
#endregion
|
||||
|
||||
private void DropDownPopup_Opened(object sender, EventArgs e)
|
||||
{
|
||||
SearchTextBox.Focus();
|
||||
SearchTextBox.SelectAll();
|
||||
EnsureParentPlacement();
|
||||
cFocusInvoker.InvokeGotFocus(this, e);
|
||||
}
|
||||
|
||||
private void DropDownPopup_Closed(object sender, EventArgs e)
|
||||
{
|
||||
searchDelayTimer.Stop();
|
||||
cFocusInvoker.InvokeLostFocus(this, e);
|
||||
}
|
||||
|
||||
private void EnsureParentPlacement()
|
||||
{
|
||||
if (ParentElement == null || ParentElement != this.Parent)
|
||||
{
|
||||
ParentElement = this.Parent as UIElement;
|
||||
|
||||
if (this.Parent is Panel panel)
|
||||
ParentIndex = panel.Children.IndexOf(this);
|
||||
else
|
||||
ParentIndex = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void SearchTextBox_TextChanged(object sender, TextChangedEventArgs e)
|
||||
{
|
||||
searchDelayTimer.Stop();
|
||||
searchDelayTimer.Start();
|
||||
}
|
||||
|
||||
private void SearchDelayTimer_Tick(object sender, EventArgs e)
|
||||
{
|
||||
searchDelayTimer.Stop();
|
||||
lastSearchText = SearchTextBox.Text ?? string.Empty;
|
||||
ApplyFilter(lastSearchText);
|
||||
}
|
||||
|
||||
private void TreeViewControl_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
|
||||
{
|
||||
if (e.NewValue is HierarchicalSelectionItem selected)
|
||||
{
|
||||
var original = ResolveOriginalItem(selected);
|
||||
if (original != null)
|
||||
SelectedItem = original;
|
||||
|
||||
DropDownPopup.IsOpen = false;
|
||||
}
|
||||
}
|
||||
|
||||
private HierarchicalSelectionItem ResolveOriginalItem(HierarchicalSelectionItem item)
|
||||
{
|
||||
if (item == null)
|
||||
return null;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(item.Id) && itemLookup.TryGetValue(item.Id, out var original))
|
||||
return original;
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
private void RebuildLookup()
|
||||
{
|
||||
itemLookup.Clear();
|
||||
|
||||
if (ItemsSource == null)
|
||||
return;
|
||||
|
||||
foreach (var entry in ItemsSource.SelectMany(item => item.SelfAndDescendants()))
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(entry.Id))
|
||||
continue;
|
||||
|
||||
itemLookup[entry.Id] = entry;
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyFilter(string searchText)
|
||||
{
|
||||
visibleItems.Clear();
|
||||
|
||||
if (ItemsSource == null)
|
||||
return;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(searchText))
|
||||
{
|
||||
foreach (var item in ItemsSource)
|
||||
{
|
||||
item.SetExpandedRecursive(false);
|
||||
visibleItems.Add(item);
|
||||
}
|
||||
|
||||
TryExpandToSelectedItem();
|
||||
return;
|
||||
}
|
||||
|
||||
var comparison = StringComparison.CurrentCultureIgnoreCase;
|
||||
|
||||
foreach (var root in ItemsSource)
|
||||
{
|
||||
var clone = root.CloneBranch(node =>
|
||||
node.FullPath?.IndexOf(searchText, comparison) >= 0 ||
|
||||
node.DisplayName?.IndexOf(searchText, comparison) >= 0);
|
||||
|
||||
if (clone == null)
|
||||
continue;
|
||||
|
||||
clone.SetExpandedRecursive(true);
|
||||
visibleItems.Add(clone);
|
||||
}
|
||||
}
|
||||
|
||||
private void TryExpandToSelectedItem()
|
||||
{
|
||||
if (SelectedItem == null || string.IsNullOrWhiteSpace(SelectedItem.Id))
|
||||
return;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(lastSearchText))
|
||||
return;
|
||||
|
||||
var chain = SelectedItem;
|
||||
while (chain != null)
|
||||
{
|
||||
chain.IsExpanded = true;
|
||||
chain = chain.Parent;
|
||||
}
|
||||
}
|
||||
|
||||
private void DisplayBorder_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
if (!IsEnabled)
|
||||
return;
|
||||
|
||||
e.Handled = true;
|
||||
DropDownPopup.IsOpen = !DropDownPopup.IsOpen;
|
||||
}
|
||||
|
||||
protected override void OnPreviewKeyDown(KeyEventArgs e)
|
||||
{
|
||||
base.OnPreviewKeyDown(e);
|
||||
|
||||
if (!IsEnabled)
|
||||
return;
|
||||
|
||||
if (!DropDownPopup.IsOpen && (e.Key == Key.Enter || e.Key == Key.Down || e.Key == Key.Space))
|
||||
{
|
||||
DropDownPopup.IsOpen = true;
|
||||
e.Handled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (DropDownPopup.IsOpen && e.Key == Key.Escape)
|
||||
{
|
||||
DropDownPopup.IsOpen = false;
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user