我有一个窗口,右边有4个按钮。当我点击其中一个按钮时,我想要显示4个弹出窗口中的一个。我只有第一个几乎完成了,但我碰到了一个我似乎无法理解的绊脚石。由于4个弹出窗口需要几乎相同,因此我决定为ContentControl
制作一个模板,然后设置我的内容并将内容控件置于弹出窗口中。我的ContentControl模板中的一项是关闭按钮。我用故事板将IsOpen
属性设置为false。这部分工作。 (这花了很长时间才发现...)但是当我再次点击按钮打开同样的Popup
它不显示,我不知道为什么。这里是我的ContentControl中的模板弹出窗口显示一次,但不是再次显示动画WPF
<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>
虽然它没有太大的关系,这里是我Popup
风格:
<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>
在我UserControl
我有这个Popup
:
<Popup x:Name="popuptest" Opened="popuptest_Opened" Closed="popuptest_Opened" Style="{DynamicResource PopupStyle}" >
<ContentControl Style="{DynamicResource PopupContentStyle}">
<b:BrightnessControl />
</ContentControl>
</Popup>
代码我用它来打开它的亮度按钮并不复杂:
private void brightButton_Click(object sender, RoutedEventArgs e)
{
popuptest.IsOpen = true;
}
和良好的措施下面是从我的XAML中其他2个事件
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);
}
请帮助:)。哦,我现在只在WPF工作了一个月左右,所以如果你看到我应该改变一切的方式来提示它。谢谢。
如果设置AutoReverse = true,会发生什么? –
@GarryVass我对你的魔法一无所知:)我没有提交我的代码,所以我必须等到明天再测试一下你的说法。 –
@GarryVass我把AutoReverse放到我的'BooleanAnimationUsingKeyFrames'中,但没有骰子。我希望有一种方法来调试,看看发生了什么。 –