feat: make sure initialize selection is handled visually.

This commit is contained in:
rabbitism
2024-09-07 00:35:51 +08:00
parent c678d82d6b
commit fb6ecc72ac

View File

@@ -200,6 +200,12 @@ public class NavMenu : ItemsControl
nav.IsHorizontalCollapsed = !collapsed; nav.IsHorizontalCollapsed = !collapsed;
} }
protected override void OnLoaded(RoutedEventArgs e)
{
base.OnLoaded(e);
TryToSelectItem(SelectedItem);
}
/// <summary> /// <summary>
/// this implementation only works in the case that only leaf menu item is allowed to select. It will be changed if we /// this implementation only works in the case that only leaf menu item is allowed to select. It will be changed if we
/// introduce parent level selection in the future. /// introduce parent level selection in the future.
@@ -224,18 +230,24 @@ public class NavMenu : ItemsControl
RaiseEvent(a); RaiseEvent(a);
return; return;
} }
var found = TryToSelectItem(newValue);
if (!found) ClearAll();
RaiseEvent(a);
}
private bool TryToSelectItem(object? item)
{
if (item is null) return false;
var leaves = GetLeafMenus(); var leaves = GetLeafMenus();
var found = false; var found = false;
foreach (var leaf in leaves) foreach (var leaf in leaves)
if (leaf == newValue || leaf.DataContext == newValue) if (leaf == item || leaf.DataContext == item)
{ {
leaf.SelectItem(leaf); leaf.SelectItem(leaf);
found = true; found = true;
} }
if (!found) ClearAll(); return found;
RaiseEvent(a);
} }
protected override bool NeedsContainerOverride(object? item, int index, out object? recycleKey) protected override bool NeedsContainerOverride(object? item, int index, out object? recycleKey)