Question

I have a StackPanel (menu) in my View and Show this control when user click a button in application bar.
I would like to LostFocus the StackPanel it closed but it does not work, you do not event triggers

XAML

<StackPanel x:Name="StackMenu" Background="{StaticResource PhoneAccentBrush}" 
            Orientation="Vertical" 
            Visibility="{Binding StackPanel_IsVisible, Mode=TwoWay, 
            Converter={StaticResource BooleanToVisibilityConverter}}"
            LostFocus="StackMenu_LostFocus">                
</StackPanel>

C#

private void StackPanel_LostFocus(object sender, RoutedEventArgs e)
{

}
Était-ce utile?

La solution

The LostFocus event is only raised when other controls outside the (StackPanel in this situation) receives focus.

Example:

  1. In the view you have your StackPanel and some TextBlock controls.
    In this case the LostFocus event will not be raised since TextBlock cannot gain Focus

  2. In the view you have your StackPanel and some TextBox controls or other input enabled controls.
    In this case the LostFocus event will be raised as soon as one of the other controls gains Focus.

Note:

If you don't have any TextBox controls, there is a workaround by catching the Tap event on the Container of the StackPanel:

  • When a Tap event occurs in the container, and the position is also in the StackPanel area, the StackPanel will catch the event and will not propagate it to the container.
  • When the Tap occurs outside of the StackPanel it will only be raised on the container or other controls present in the container (this is when your StackPanel loses Focus).
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top