2016-04-03 81 views
0

我有一个ToggleButton打开一个弹出窗口,并有一个ItemsControlPopupDataTemplate中的故事板

我想在项目控件中点击物品时隐藏弹出窗口。

<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> 

并为此设置了一个故事。

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

,但是当我用这个,我得到以下错误

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

+0

除此之外,我认为你的做法是不是一个好(我发现了自己不久前)。属性动画的问题在于它没有真正设置属性值,而是覆盖了有效值。结果有两种情况是可能的:如果动画的FillBehavior设置为HoldEnd,动画将阻止弹出窗口重新打开(IsOpen有效值将保持为false),否则(如果设置为Stop ')有效的“IsOpen”值将恢复为“true”,导致弹出窗口在动画结束后重新打开。 – Grx70

回答

0

这取决于在哪里定义Stroryboard,所以我会假设它在Window.Resources或类似的东西。您使用TargetName作为LeaveButtonTargetProperty作为IsOpen。您要么LeaveButtonIsCheckedLeavePopupIsOpen。也可以尝试改变TargetNameTarget并使用从解决你得到的错误结合

<Storyboard x:Key="HideLeavePopup" 
      Storyboard.Target="{Binding ElementName=LeaveButton}" 
      Storyboard.TargetProperty="IsChecked"> 
    <BooleanAnimationUsingKeyFrames> 
     <DiscreteBooleanKeyFrame KeyTime="00:00:00.1" Value="False" /> 
    </BooleanAnimationUsingKeyFrames> 
</Storyboard> 
+0

谢谢,我使用这个,但再次得到错误。 –

+0

什么错误?和你的问题一样?故事板在哪里定义? – dkozl

+0

非常感谢...没关系:) –

1

你尝试this?

内,您的故事板,而不是

Storyboard.TargetName="LeaveButton" 

使用

Storyboard.Target="{Binding ElementName=LeaveButton}" 
+0

谢谢,但我得到的错误:IsChecked:真名不能以范围的名义来源 –

+0

另一个答案说得很清楚。它应该是IsOpen属性而不是IsChecked。 –