我有一个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名称
答案 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>