Page 1 of 1

SetFocusAction TargetName only works when inside the target

Posted: 12 Apr 2021, 18:11
by sgonchar
Hello,
I noticed that:
      <i:EventTrigger EventName="IsVisibleChanged">
        <noesis:SetFocusAction TargetName="SomeButton" />
      </i:EventTrigger>
Only works when is part of triggers for SomeButton, and not a panel, or another element.
I feel like I'm just doing something wrong ..

This works fine:
<StackPanel>
    <Button x:Name="SomeButton" Style="{StaticResource SomeButtonStyle}" >
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="IsVisibleChanged">
                <noesis:SetFocusAction TargetName="SomeButton" />  <!--  TargetName optional here -->
            </i:EventTrigger>
         </i:Interaction.Triggers>
    </Button>
</StackPanel>
This does not work:
<StackPanel
      <i:Interaction.Triggers>
           <i:EventTrigger EventName="IsVisibleChanged">
               <noesis:SetFocusAction TargetName="SomeButton" />
           </i:EventTrigger>
      </i:Interaction.Triggers>
    >
    <Button x:Name="SomeButton" Style="{StaticResource SomeButtonStyle}" >
    </Button>
</StackPanel>
What am I doing wrong?

Re: SetFocusAction TargetName only works when inside the target

Posted: 14 Apr 2021, 14:11
by sfernandez
What is happening is that the IsVisibleChanged event is raised in the parent StackPanel before updating the visibility of the child Button, so when the SetFocusAction tries to set the focus on the Button it fails because it is still not focusable (not visible).

When moving the trigger to the Button, the IsVisibleChanged event is happening once the Button is visible and focusable, thus the action works as expected.

It has nothing to do with the TargetName property, because I can have another child inside the StackPanel after the Button with the trigger and SetFocusAction using the TargetName to point to the Button and it also works:
<StackPanel x:Name="LayoutRoot" Visibility="Hidden" HorizontalAlignment="Center" VerticalAlignment="Center">
  <Button x:Name="SomeButton" Content="Some Button" Margin="10"/>
  <Button x:Name="OtherButton" Content="Other Button" Margin="10"/>
  <Decorator>
    <i:Interaction.Triggers>
      <i:EventTrigger EventName="IsVisibleChanged">
        <noesis:SetFocusAction TargetName="SomeButton"/>
      </i:EventTrigger>
    </i:Interaction.Triggers>
  </Decorator>
</StackPanel>

Re: SetFocusAction TargetName only works when inside the target

Posted: 15 Apr 2021, 16:59
by sgonchar
Thank you, I never realized visible and triggers processing is sequential like that.

Re: SetFocusAction TargetName only works when inside the target

Posted: 19 Apr 2021, 10:45
by sfernandez
Yes, behaviors and triggers are attached to the associated object as they appear in the tree, so the registration of events follows the same order.