This commit is contained in:
DismissedLight
2023-09-13 20:56:07 +08:00
parent 2ff8d6d7f4
commit 706f894ea5

View File

@@ -14,12 +14,31 @@ namespace Snap.Hutao.Control.Behavior;
[DependencyProperty("CommandParameter", typeof(object))]
internal sealed partial class InvokeCommandOnLoadedBehavior : BehaviorBase<UIElement>
{
private bool executed;
protected override void OnAttached()
{
base.OnAttached();
// FrameworkElement in a ItemsRepeater gets attached twice
if (!executed && AssociatedObject is FrameworkElement { IsLoaded: true })
{
TryExecuteCommand();
}
}
/// <inheritdoc/>
protected override void OnAssociatedObjectLoaded()
{
TryExecuteCommand();
}
private void TryExecuteCommand()
{
if (Command is not null && Command.CanExecute(CommandParameter))
{
Command.Execute(CommandParameter);
executed = true;
}
}
}