kategorie fix highlight current value
This commit is contained in:
@@ -115,6 +115,7 @@ namespace FasdDesktopUi.Basics.UserControls
|
||||
searchTextBox?.Focus();
|
||||
searchTextBox?.SelectAll();
|
||||
suppressTreeSelectionChanged = false;
|
||||
SyncTreeSelectionWithSelectedItem(bringIntoView: true);
|
||||
LogEntry($"[CategoryPicker] DropDownOpened. Selected={SelectedItem?.FullPath ?? "<null>"}");
|
||||
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<HierarchicalSelectionItem> 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)
|
||||
|
||||
Reference in New Issue
Block a user