请用Popup帮我

时间:2010-07-30 14:52:57

标签: wpf popup

我有一个图像和一个弹出窗口。单击图像弹出窗口时应该打开。

我是这样开始的,现在我卡住了。

        <Image x:Name="LockImage" Source="/Lock.png">
            <Image.Triggers>
                <EventTrigger RoutedEvent="MouseDown">
                    // ?????? WHAT's here?
                </EventTrigger>
            </Image.Triggers>
        </Image>
        <Popup x:Name="LockPopup" PlacementTarget="{Binding ElementName=LockImage}">
            <TextBlock Text="This is a popup" />
        </Popup>

UPD ...哎呀,实际上我忘记了......我希望弹出窗口不会立即显示,而是在一两秒之后显示。如果它只是一次点击,它将是其他东西......(默认动作)

1 个答案:

答案 0 :(得分:2)

这是您想要做的解决方案。延迟时间可以在故事板定义中设置。将此代码插入到新的wpf应用程序项目Window.xaml文件中。

<Window.Resources>
        <Storyboard x:Key="ShowPopup">
            <BooleanAnimationUsingKeyFrames Storyboard.TargetName="LockPopup" Storyboard.TargetProperty="(Popup.IsOpen)">
                <DiscreteBooleanKeyFrame KeyTime="00:00:00.5" Value="True" />
            </BooleanAnimationUsingKeyFrames>
        </Storyboard>

        <Storyboard x:Key="HidePopup" Storyboard.TargetName="LockPopup" Storyboard.TargetProperty="(Popup.IsOpen)">
            <BooleanAnimationUsingKeyFrames>
                <DiscreteBooleanKeyFrame KeyTime="00:00:00.5" Value="False" />
            </BooleanAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>
    <Grid x:Name="grid" ShowGridLines="True">
        <Image x:Name="LockImage" Stretch="None" >
            <Image.Source>
                <DrawingImage>
                    <DrawingImage.Drawing>
                        <GeometryDrawing Brush="Black">
                            <GeometryDrawing.Geometry>
                                <EllipseGeometry RadiusX="10" RadiusY="10"/>
                            </GeometryDrawing.Geometry>
                        </GeometryDrawing>
                    </DrawingImage.Drawing>
                </DrawingImage>
            </Image.Source>
            <Image.Triggers>
                <EventTrigger RoutedEvent="Image.MouseLeftButtonDown">
                    <BeginStoryboard Storyboard="{StaticResource ShowPopup}"/>
                </EventTrigger>
                <EventTrigger RoutedEvent="Image.MouseLeave">
                    <BeginStoryboard Storyboard="{StaticResource HidePopup}"/>
                </EventTrigger>
            </Image.Triggers>
        </Image>
        <Popup x:Name="LockPopup" PlacementTarget="{Binding ElementName=LockImage}" DataContext="{Binding}" Placement="Bottom">
            <TextBlock Text="This is a popup" Background="White" Foreground="Black" />
        </Popup>       
    </Grid>