I have a window with 4 buttons on the right. When I click on one of the buttons, I want to show 1 of 4 pop-ups. I have only the 1st one, almost finished, but I hit a stumbling block that I cannot understand. Since the 4 pop-ups should have been almost identical, I decided to create a template for the ContentControl , and then set my content in it and put the content control in the pop-up window. One of the elements of my ContentControl template is the close button. I used the storyboard to set the IsOpen property to false. So this part works. (It took a long time to understand ...), but when I press the button again to open the same Popup , it does not appear, and I'm not sure why. Here is my ContentControl template
<Style x:Key="PopupContentStyle" TargetType="ContentControl"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ContentControl"> <Grid> <Rectangle Fill="WhiteSmoke" Opacity=".50" Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, Path=Width}" Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, Path=Height}" /> <Button Height="50" Style="{DynamicResource CloseButton}" HorizontalAlignment="Right" VerticalAlignment="Top" > <Button.Triggers> <EventTrigger RoutedEvent="Button.Click"> <BeginStoryboard> <Storyboard> <BooleanAnimationUsingKeyFrames Storyboard.Target="{Binding RelativeSource={RelativeSource AncestorLevel=1, AncestorType=Popup,Mode=FindAncestor}}" Storyboard.TargetProperty="(Popup.IsOpen)" Duration="0:0:0"> <DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="False" /> </BooleanAnimationUsingKeyFrames> </Storyboard> </BeginStoryboard> </EventTrigger> </Button.Triggers> </Button> <ContentPresenter /> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>
although it doesn't really matter, here is my Popup style:
<Style x:Key="PopupStyle" TargetType="{x:Type Popup}"> <Setter Property="AllowsTransparency" Value="True"/> <Setter Property="PopupAnimation" Value="Fade"/> <Setter Property="Placement" Value="Center"/> <Setter Property="PlacementTarget" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"/> </Style>
In my UserControl , I have this Popup :
<Popup x:Name="popuptest" Opened="popuptest_Opened" Closed="popuptest_Opened" Style="{DynamicResource PopupStyle}" > <ContentControl Style="{DynamicResource PopupContentStyle}"> <b:BrightnessControl /> </ContentControl> </Popup>
The code I use to open it for the brightness button is not complicated:
private void brightButton_Click(object sender, RoutedEventArgs e) { popuptest.IsOpen = true; }
and for good measure there are other 2 events from my xaml
public event PopupIsOpenedChangedHandler PopupIsOpenedChanged; public delegate void PopupIsOpenedChangedHandler(bool isOpen); private void OnPopupIsOpenedChanged(bool isOpen) { if (PopupIsOpenedChanged != null) PopupIsOpenedChanged(isOpen); } private void popuptest_Opened(object sender, System.EventArgs e) { OnPopupIsOpenedChanged(popuptest.IsOpen); }
Please, help:). Oh, and I've been working with WPF for about a month now, so if you see something that I have to change, suggest it. Thanks.