DataTemplate中的故事板

时间:2016-04-03 06:23:06

标签: wpf xaml

我有一个ToggleButton打开一个弹出窗口,并在ItemsControl中有一个Popup

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

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

但是当我使用它时,我得到以下错误

  

在“System.Windows.Control.Button”类型的名称范围内找不到LeaveButton名称

2 个答案:

答案 0 :(得分:1)

您是否尝试过this?

在故事板内部,而不是

Storyboard.TargetName="LeaveButton"

使用

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

答案 1 :(得分:0)

这取决于Storyboard的定义,所以我假设它在Window.Resources或类似的东西。您将TargetName用作 LeaveButton ,将TargetProperty用作 IsOpen 。您需要 LeaveButton IsChecked LeavePopup IsOpen 。同时尝试将TargetName更改为Target并使用绑定:

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