I have a flyout with a cancel button, where I want the "Cancel" click handler to Hide() the parent flyout. The code segment (below) is part of a ListBoxItem, so I can't reference the flyout by x:name="flyout_name".
This means I need to traverse the Visual Tree Hierarchy, but I'm having trouble accessing the parent Controls::Flyout object.
<Button x:Name="DeleteColButton" Content="Delete">
<Button.Flyout>
<Flyout>
<Flyout.FlyoutPresenterStyle>
<Style TargetType="FlyoutPresenter">
<Setter Property="ScrollViewer.HorizontalScrollMode" Value="Disabled"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
<Setter Property="IsTabStop" Value="True"/>
<Setter Property="TabNavigation" Value="Cycle"/>
</Style>
</Flyout.FlyoutPresenterStyle>
<StackPanel>
<TextBlock TextWrapping="Wrap" Text="Are you sure?"/>
<Button Click="DeleteColClickHandler">Yes</Button>
<Button Click="DeleteColCancelClickHandler">Cancel</Button>
</StackPanel>
</Flyout>
</Button.Flyout>
</Button>
I can go from the "Cancel" Button to the top-level stackpanel in the flyout, but the next level up is neither Controls::Flyout nor Controls::FlyoutPresenter. I can call panel.GetParent() 11 times after "escaping" the top-level stackpanel, and none of those DependencyObjects show up as Flyout, FlyoutPresenter, StackPanel, ListBoxItem, or ListBox. Is the visual hierarchy of Flyout somehow disconnected from the hierarchy I'm tracing through the XAML?
I've heard that Flyout doesn't have an associated DependencyObject in the visual tree.. Is this really the case? If I try to go top-down, how would I find the DependencyObject of a ListBoxItem?
Should I switch to Dialog?
Edit: I tried to get access to the button for ( Button().Flyout().Hide() ) from the ListBoxItem, but it proved too difficult.
However, using GetOpenPopups() to get the list of open popups - and closing them all - seemed to work. Thank you!