带按钮的弹出窗口,有时会忽略点击次数

时间:2014-07-09 10:23:00

标签: c# wpf popup

发现另一个奇怪的Popup行为:当显示弹出窗口时,弹出窗口上的控件(和弹出窗口本身?)开始工作之前有一定的延迟。即谈论按钮点击。

这是一个测试项目(复制粘贴并查看)。

XAML:

<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">
    <StackPanel x:Name="panel" Orientation="Horizontal" VerticalAlignment="Top">
        <Button x:Name="button1" Width="100" Height="100"/>
        <Button x:Name="button2" Width="100" Height="100"/>
        <Popup x:Name="popup" StaysOpen="False" AllowsTransparency="True" Placement="Relative">
            <Grid x:Name="grid" Width="150" Height="150" Background="Yellow">
                <Button x:Name="button3" Width="100" Height="100"/>
            </Grid>
        </Popup>
    </StackPanel>
</Window>

代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        // create timer to open popup after hovering button 1 or 2
        var timer = new Timer(500) { AutoReset = false };
        timer.Elapsed += (s, o) => Dispatcher.Invoke(() => popup.IsOpen = true);
        // button hover events
        button1.MouseMove += (s, o) => { popup.PlacementTarget = button1; timer.Start(); };
        button2.MouseMove += (s, o) => { popup.PlacementTarget = button2; timer.Start(); };
        button1.MouseLeave += (s, o) => { timer.Stop(); };
        button2.MouseLeave += (s, o) => { timer.Stop(); };
        // popup event
        popup.MouseMove += (s, o) =>
        {
            // if outside of popup grid region - close
            if (!new Rect(grid.RenderSize).Contains(o.GetPosition(grid)))
                popup.IsOpen = false;
        };
        // clicks
        button1.Click += (s, o) => Title += "1";
        button2.Click += (s, o) => Title += "2";
        button3.Click += (s, o) => Title += "3";
        this.MouseDown += (s, o) => { Title += "w"; o.Handled = true; };
        panel.MouseDown += (s, o) => { Title += "p"; o.Handled = true; };
        grid.MouseDown += (s, o) => { Title += "g"; o.Handled = true; };
    }
}

在悬停按钮后会出现弹出窗口,并在将鼠标移出弹出区域后消失。

如果正确计时弹出按钮(当出现弹出窗口时正确),则有两个可能的问题:

  • 没有弹出式按钮点击事件发生(弹出窗口可见,按钮似乎被点击),它被吞噬;
  • 弹出窗口不会捕获鼠标(它会一直停留在屏幕上,直到通过单击弹出按钮获得焦点),见图:

enter image description here

有关正在发生的事情以及如何接收可靠的弹出按钮点击的任何想法?

0 个答案:

没有答案