我有一个窗口,在右侧有4个按钮。当我点击其中一个按钮时,我想要显示4个弹出框中的1个。我只完成了第一个,但是我遇到了一个问题,我似乎无法解决。因为这4个弹出框几乎完全相同,所以我决定为
尽管这里并不太重要,但是这是我的“弹出窗口”样式:
在我的
为了保险起见,这是我XAML中的另外两个事件。
ContentControl
制作一个模板,然后在其中设置我的内容,并将内容控件放入弹出框中。我的ContentControl模板中的一个项目是关闭按钮。我使用故事板将IsOpen
属性设置为false。所以这部分可以正常工作。(这花费了很长时间才解决...)但是当我再次点击该Popup
打开同样的弹出框时,它不会显示,我不确定为什么。以下是我的ContentControl模板。<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>
在我的
用户控件
中,我有这个弹出窗口
:<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中的另外两个事件。
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工作了一个月,如果您看到需要改变的地方,请务必建议。谢谢。
BooleanAnimationUsingKeyFrames
中加入了 AutoReverse,但是没有效果。我希望有一种调试方法可以查看发生了什么。 - Robert Snyder