【问题标题】:Silverlight: Showing popup whenever mouse is over the owner element or over the popup itselfSilverlight:只要鼠标悬停在所有者元素或弹出窗口本身上,就会显示弹出窗口
【发布时间】:2011-07-04 10:12:06
【问题描述】:

我正在尝试构建一个弹出窗口,当用户将鼠标悬停在元素上时会显示该弹出窗口。如果用户将鼠标移到弹出窗口上,我希望它保持显示状态。但是,如果用户同时离开了 owner 元素和 popup 元素,那么 popup 元素应该会消失。

我尝试了以下方法,但它不起作用:

<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<StackPanel MouseEnter="OnMouseEnter" MouseMove="OnMouseMove" MouseLeave="OnMouseLeave">
    <HyperlinkButton Content="Root" HorizontalAlignment="Left"/>
    <Popup x:Name="popup" MouseEnter="OnMouseEnter" MouseMove="OnMouseMove"  MouseLeave="OnMouseLeave">
        <StackPanel x:Name="leaves" HorizontalAlignment="Left">
            <HyperlinkButton Content="Leaf1" />
            <HyperlinkButton Content="Leaf2" />
        </StackPanel>
    </Popup>
</StackPanel>

后面的代码:

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
    }

    private void OnMouseMove(object sender, MouseEventArgs e)
    {
        this.popup.IsOpen = true;
    }

    private void OnMouseEnter(object sender, MouseEventArgs e)
    {
        this.popup.IsOpen = true;
    }

    private void OnMouseLeave(object sender, MouseEventArgs e)
    {
        this.popup.IsOpen = false;
    }
}

发生的情况是所有者元素 (Root) 的 MouseLeave 被触发并且 Popup 被隐藏。

有什么想法吗?

【问题讨论】:

    标签: silverlight popup mouseover


    【解决方案1】:

    使用 DispatcherTimer 将 IsOpen 设置为 false,如下所示:-

    public partial class MainPage: UserControl
    {
        DispatcherTimer popupTimer = new DispatcherTimer();
    
        public MainPage()
        {
            InitializeComponent();
    
            popupTimer.Interval = TimeSpan.FromMilliseconds(100);
            popupTimer.Tick += new EventHandler(popupTimer_Tick);
        }
    
        void popupTimer_Tick(object sender, EventArgs e)
        {
            popupTimer.Stop();
            popup.IsOpen = false;
        }
    
        private void OnMouseEnter(object sender, MouseEventArgs e)
        {
            popupTimer.Stop();
            popup.IsOpen = true;
        }
    
        private void OnMouseLeave(object sender, MouseEventArgs e)
        {
            popupTimer.Start();
        }
    }
    

    还将 MouseEnter MouseMove 事件从Popup 移到弹出窗口内的StackPanel 上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-22
      • 2011-06-11
      • 2019-10-14
      • 2022-12-19
      相关资源
      最近更新 更多