WPF popup gets disappear magically

WPF popup is not working as expected for me. So the behavior is show popup when mouse enters on "Test: button and after that Clicking on "Close" button which is on popup will hide the popup. Everything works fine until I left click on "Test" button. After that the Mouse event event gets fired on "Test" button but the popup does not show up.

It would be great if someone will be able to help me out here.

Thanks, Code

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>

    <Grid Margin="10">
        <Button x:Name="btnTest"  Width="100" Height="25" 
                MouseEnter="btnTest_MouseEnter">Test Button</Button>
        <Popup Name="MyPopup"
               PlacementTarget="{Binding ElementName=btnTest}"
               Placement="Mouse"
               StaysOpen="False">
            <StackPanel Background="PaleGreen">
                <Label HorizontalAlignment="Center">I am a popup</Label>
                <Button Click="Hide_Click" Content="Close"/>
            </StackPanel>
        </Popup>
    </Grid>
</Grid>

Code behind

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
    private void Show_Click(object sender, RoutedEventArgs e)
    {
        MyPopup.IsOpen = true;
    }
    private void Hide_Click(object sender, RoutedEventArgs e)
    {
        MyPopup.IsOpen = false;
    }
    private void btnTest_MouseEnter(object sender, MouseEventArgs e)
    {
        MyPopup.IsOpen = true;
    }
}
}

Answers


I've tried your code, and I do not understand why it worked. However, I tried to implement your desired functionality with triggers, etc. I've tested it and it seems to work:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid Margin="10">
        <Button x:Name="btnTest"  Width="100" Height="25" Content="Test Button">
            <Button.Triggers>
                <EventTrigger RoutedEvent="Button.MouseEnter">
                    <BeginStoryboard>
                        <Storyboard>
                            <BooleanAnimationUsingKeyFrames 

            Storyboard.TargetName="MyPopup" 
            Storyboard.TargetProperty="IsOpen"
            Duration="0:0:1" FillBehavior="HoldEnd">
                                <DiscreteBooleanKeyFrame Value="True" KeyTime="0:0:0" />
                            </BooleanAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Button.Triggers>
        </Button>
        <Popup Name="MyPopup"
           PlacementTarget="{Binding ElementName=btnTest}"
           Placement="Mouse"
           StaysOpen="False">
            <StackPanel Background="PaleGreen">
                <Label HorizontalAlignment="Center">I am a popup</Label>
                <Button  Content="Close">
                    <Button.Triggers>
                        <EventTrigger RoutedEvent="Button.Click">
                            <BeginStoryboard>
                                <Storyboard>
                                    <BooleanAnimationUsingKeyFrames                 
            Storyboard.TargetName="MyPopup" 
            Storyboard.TargetProperty="IsOpen"
            Duration="0:0:1" FillBehavior="HoldEnd">
                                        <DiscreteBooleanKeyFrame Value="False" KeyTime="0:0:0" />
                                    </BooleanAnimationUsingKeyFrames>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </Button.Triggers>
                </Button>
            </StackPanel>
        </Popup>
    </Grid>
</Grid>
</Window>

I'm not sure if there is a shorter way; probably there is.


Need Your Help

What are some choice Python programming environments?

python development-environment

I'm wondering if there's a more beginner-friendly environment to write Python than a terminal shell. Any suggestions?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.