Background
I have a control template for a HeaderedContentControl
which contains a button as the header, which opens a popup containing the main content when clicked (via an EventTrigger
).
This works as I expect, until I try to add a button to the popup which collapses the control. The main button (the header) disappears, but the popup (the content) stays where it is until I take mouse focus from it (by clicking somewhere).
Here's a shortened example demonstrating the issue:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="WpfTest.View"
Height="300" Width="400" >
<Window.Resources>
<ControlTemplate x:Key="ControlTemplate" TargetType="{x:Type HeaderedContentControl}" >
<Grid Width="100" Height="25" >
<Button Content="{TemplateBinding Header}" >
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click" >
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetName="Popup"
Storyboard.TargetProperty="(Popup.IsOpen)" >
<DiscreteBooleanKeyFrame KeyTime="0" Value="True" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
<Popup x:Name="Popup" StaysOpen="False" >
<ContentPresenter Content="{TemplateBinding Content}" />
</Popup>
</Grid>
</ControlTemplate>
</Window.Resources>
<HeaderedContentControl x:Name="MainControl" Template="{StaticResource ControlTemplate}" Header="Show Popup" >
<HeaderedContentControl.Content>
<Border Background="White" BorderThickness="1" BorderBrush="Black" >
<Button Margin="2" Content="Hide All" >
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click" >
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="MainControl"
Storyboard.TargetProperty="(UIElement.Visibility)" >
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Collapsed}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
</Border>
</HeaderedContentControl.Content>
</HeaderedContentControl>
</Window>
In my real code, the ControlTemplate
isn't in the same place as the control's usage. It's in a ResourceDictionary
somewhere else entirely, and shouldn't know anything about the contents of its popup - it passes it straight onto a ContentPresenter
.
Question
Is there a way, ideally without letting the ControlTemplate
know anything about the possibility of the popup containing a "Hide" button, of making the popup disappear when the HeaderedContentControl
is collapsed?
What I've tried
- Adding another trigger to the
HeaderedContentControl
to watch forIsVisible
changing. There's no appropriateEventTrigger
and other triggers can't contain element names. - Adding an event handler to
IsVisibleChanged
in a subclass ofHeaderedContentControl
. The event fires, butPopup.IsOpen
remainstrue
after being set tofalse
.