1

I have a ToggleButton that open a popup, and have a ItemsControl in Popup.

I want to hide popup when click on items in items control.

<ToggleButton Content="?????" x:Name="LeaveButton" Style="{StaticResource ToggleButtonImageStyle}" Padding="13"/>
<Popup 
   KeyDown="UIElement_OnKeyDown" 
   Opened="SubMenuPopup_OnOpened" 
   IsOpen="{Binding IsChecked, ElementName=LeaveButton}" 
   StaysOpen="False" 
   x:Name="LeavePopup" 
   AllowsTransparency="True" 
   PopupAnimation="Fade" 
   PlacementTarget="{Binding ElementName=LeaveButton}" 
   Placement="Right">
   <StackPanel Orientation="Horizontal" Margin="15">
      <Polygon Points="15 15,0 30,15 45" Fill="{DynamicResource HeaderBackgroundBrush}" />
      <StackPanel  Width="250">
         <ItemsControl ItemsSource="{Binding WorkshopList}">
            <ItemsControl.ItemTemplate>
               <DataTemplate>
                  <Button 
                     Content="{Binding Name}"
                     Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.LeaveCommand}" 
                     CommandParameter="{Binding Id}" 
                     Style="{StaticResource ButtonImageTextStyle}" 
                     Padding="20">
                     <Button.Triggers>
                        <EventTrigger RoutedEvent="ButtonBase.Click">
                           <BeginStoryboard Storyboard="{StaticResource HideLeavePopup}" />
                        </EventTrigger>
                     </Button.Triggers>
                  </Button>
               </DataTemplate>
            </ItemsControl.ItemTemplate>
         </ItemsControl>
      </StackPanel>
   </StackPanel>
</Popup>

and set a story for this.

<Storyboard x:Key="HideLeavePopup" Storyboard.TargetName="LeaveButton" Storyboard.TargetProperty="IsOpen">
   <BooleanAnimationUsingKeyFrames>
      <DiscreteBooleanKeyFrame KeyTime="00:00:00.1" Value="False" />
   </BooleanAnimationUsingKeyFrames>
</Storyboard>

but when I use this, I get following error

LeaveButton name can not be found in the name scope of type 'System.Windows.Control.Button'

dkozl
  • 32,814
  • 8
  • 87
  • 89
ar.gorgin
  • 4,765
  • 12
  • 61
  • 100
  • Aside from resolving the error you're getting, I think that your approach is not a good one (I've found that out myself not long ago). The problem with property animation is that it doesn't actually set the property value, but rather overrides the effective value. As a result two scenarios are possible: if animation's `FillBehavior` is set to `HoldEnd`, the animation will prevent the popup from reopening (the `IsOpen` effective value will be held `false`), else (if set to `Stop`) the effective `IsOpen` value will revert back to `true`, causing the popup to reopen once the animation is over. – Grx70 Apr 03 '16 at 08:08

2 Answers2

1

Did you try this?

Inside of your storyboard, Instead of

Storyboard.TargetName="LeaveButton"

use

Storyboard.Target="{Binding ElementName=LeaveButton}"
Community
  • 1
  • 1
Akshay Jain
  • 884
  • 5
  • 19
0

It depends where Storyboard is defined so I'll assume it's in Window.Resources or something alike. You use TargetName as LeaveButton and TargetProperty as IsOpen. You want either LeaveButton and IsChecked or LeavePopup and IsOpen. Also try changing TargetName to Target and use binding:

<Storyboard x:Key="HideLeavePopup" 
            Storyboard.Target="{Binding ElementName=LeaveButton}"
            Storyboard.TargetProperty="IsChecked">
    <BooleanAnimationUsingKeyFrames>
        <DiscreteBooleanKeyFrame KeyTime="00:00:00.1" Value="False" />
    </BooleanAnimationUsingKeyFrames>
</Storyboard>
Igor Popov
  • 9,795
  • 7
  • 55
  • 68
dkozl
  • 32,814
  • 8
  • 87
  • 89