C# WPF弹出窗口神奇地消失了

C# WPF弹出窗口神奇地消失了,c#,wpf,button,popup,C#,Wpf,Button,Popup,WPF弹出窗口未按预期为我工作。因此,当鼠标进入“测试:按钮”时显示弹出窗口,然后单击弹出窗口上的“关闭”按钮将隐藏弹出窗口。在我左键单击“测试”按钮之前,一切正常。之后,“测试”按钮将触发鼠标事件,但弹出窗口不会显示 如果有人能在这里帮助我,那就太好了 谢谢, 代码 测试按钮 我是一个弹出窗口 代码隐藏 using System; using System.Collections.Generic; using System.Windows; using System.Windows.In

WPF弹出窗口未按预期为我工作。因此,当鼠标进入“测试:按钮”时显示弹出窗口,然后单击弹出窗口上的“关闭”按钮将隐藏弹出窗口。在我左键单击“测试”按钮之前,一切正常。之后,“测试”按钮将触发鼠标事件,但弹出窗口不会显示

如果有人能在这里帮助我,那就太好了

谢谢, 代码


测试按钮
我是一个弹出窗口

代码隐藏

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;
    }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Windows;
使用System.Windows.Input;
命名空间WpfApplication1
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
私有无效显示\单击(对象发送者,路由目标)
{
MyPopup.IsOpen=true;
}
私有无效隐藏\单击(对象发送者,路由目标)
{
MyPopup.IsOpen=false;
}
私有void btnTest_MouseEnter(对象发送方,MouseEventArgs e)
{
MyPopup.IsOpen=true;
}
}
}

我已经尝试了你的代码,但我不明白为什么它会起作用。 但是,我尝试用触发器等实现您想要的功能。 我已经对它进行了测试,它似乎有效:

<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>

我是一个弹出窗口

我不确定是否有较短的方法;可能有。

您的测试按钮没有点击事件……或者我错过了什么吗?@steve虽然没有点击回调,但您应该能够看到解释的行为。您说过,在您点击按钮之前,一切都正常。但是按钮点击并没有触发事件打开弹出窗口和StaysOpen=“False”。那么你到底期望什么呢?@Steve所以当鼠标进入“测试”按钮时,弹出窗口应该会打开。一旦你点击“测试”按钮,这不会发生。1.将鼠标指针指向“测试”按钮,你会看到弹出窗口。2.点击“测试”按钮。3.再次将鼠标指针指向“测试”按钮“按钮和弹出窗口现在不会显示。谢谢。它很好用。顺便说一句,像这样的事情应该是开箱即用的。很抱歉,没有足够的声誉不让我给你这个信用。
<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>