From e8584c1453bec6c57182329a644b3285c8cadd9d Mon Sep 17 00:00:00 2001 From: Meik Date: Mon, 9 Feb 2026 20:43:47 +0100 Subject: [PATCH] kategorie fix highlight current value --- .../HierarchicalSelectionControl.xaml.cs | 92 ++++++++++++++++++ .../Pages/SettingsPage/SettingsPageView.xaml | 95 ++++++++++++------- 2 files changed, 151 insertions(+), 36 deletions(-) diff --git a/FasdDesktopUi/Basics/UserControls/HierarchicalSelectionControl.xaml.cs b/FasdDesktopUi/Basics/UserControls/HierarchicalSelectionControl.xaml.cs index 8a84761..b79ab7f 100644 --- a/FasdDesktopUi/Basics/UserControls/HierarchicalSelectionControl.xaml.cs +++ b/FasdDesktopUi/Basics/UserControls/HierarchicalSelectionControl.xaml.cs @@ -115,6 +115,7 @@ namespace FasdDesktopUi.Basics.UserControls searchTextBox?.Focus(); searchTextBox?.SelectAll(); suppressTreeSelectionChanged = false; + SyncTreeSelectionWithSelectedItem(bringIntoView: true); LogEntry($"[CategoryPicker] DropDownOpened. Selected={SelectedItem?.FullPath ?? ""}"); DropDownOpened?.Invoke(this, e); } @@ -203,6 +204,7 @@ namespace FasdDesktopUi.Basics.UserControls } TryExpandToSelectedItem(); + SyncTreeSelectionWithSelectedItem(bringIntoView: false); return; } @@ -220,6 +222,9 @@ namespace FasdDesktopUi.Basics.UserControls clone.SetExpandedRecursive(true); visibleItems.Add(clone); } + + // If the selected item is part of current results, keep it visually selected. + SyncTreeSelectionWithSelectedItem(bringIntoView: false); } private void TryExpandToSelectedItem() @@ -238,6 +243,93 @@ namespace FasdDesktopUi.Basics.UserControls } } + private void SyncTreeSelectionWithSelectedItem(bool bringIntoView) + { + if (SelectedItem == null || string.IsNullOrWhiteSpace(SelectedItem.Id)) + return; + + EnsureTemplateParts(); + if (treeViewControl == null) + return; + + // Wait for popup/template layout to finish so item containers are generated. + Dispatcher.BeginInvoke(new Action(() => + { + var target = FindVisibleItemById(VisibleItems, SelectedItem.Id); + if (target == null) + return; + + var ancestor = target.Parent; + while (ancestor != null) + { + ancestor.IsExpanded = true; + ancestor = ancestor.Parent; + } + + treeViewControl.UpdateLayout(); + var targetContainer = GetTreeViewItemContainer(treeViewControl, target); + if (targetContainer == null) + return; + + suppressTreeSelectionChanged = true; + try + { + targetContainer.IsSelected = true; + if (bringIntoView) + targetContainer.BringIntoView(); + } + finally + { + suppressTreeSelectionChanged = false; + } + }), DispatcherPriority.Loaded); + } + + private static HierarchicalSelectionItem FindVisibleItemById(IEnumerable items, string id) + { + if (items == null || string.IsNullOrWhiteSpace(id)) + return null; + + foreach (var item in items) + { + if (item == null) + continue; + + if (string.Equals(item.Id, id, StringComparison.OrdinalIgnoreCase)) + return item; + + var childMatch = FindVisibleItemById(item.Children, id); + if (childMatch != null) + return childMatch; + } + + return null; + } + + private static TreeViewItem GetTreeViewItemContainer(ItemsControl root, object targetItem) + { + if (root == null || targetItem == null) + return null; + + var directContainer = root.ItemContainerGenerator.ContainerFromItem(targetItem) as TreeViewItem; + if (directContainer != null) + return directContainer; + + foreach (var child in root.Items) + { + var childContainer = root.ItemContainerGenerator.ContainerFromItem(child) as TreeViewItem; + if (childContainer == null) + continue; + + childContainer.UpdateLayout(); + var result = GetTreeViewItemContainer(childContainer, targetItem); + if (result != null) + return result; + } + + return null; + } + private void EnsureTemplateParts() { if (treeViewControl == null) diff --git a/FasdDesktopUi/Pages/SettingsPage/SettingsPageView.xaml b/FasdDesktopUi/Pages/SettingsPage/SettingsPageView.xaml index b07b8d8..ec22226 100644 --- a/FasdDesktopUi/Pages/SettingsPage/SettingsPageView.xaml +++ b/FasdDesktopUi/Pages/SettingsPage/SettingsPageView.xaml @@ -1,29 +1,30 @@ - + @@ -203,25 +204,39 @@ SelectedCountryCode="GB" /> - + - + + Margin="0,3,0,10"> - + - + - - + - + -