扩展鼠标区域以包括按钮和放大器。新的弹出窗口

时间:2016-10-16 03:17:35

标签: c# wpf xaml popup datatemplate

我有一个弹出窗口直接出现在Mouseover上的按钮旁边。当鼠标从按钮跳到弹出窗口时,我需要保持弹出窗口打开。当按钮离开按钮或弹出窗口时,我还需要关闭弹出窗口。它与this帖子类似,所以请原谅我,但那里没有提供有效的解决方案。

本质上,我想扩展mouseleave区域以包括按钮和弹出窗口,这样如果鼠标离开任何一个鼠标,鼠标就可以在两个区域上移动并且鼠标移动。

        <Popup x:Key="CustomPopup" x:Name="samplePopup" Margin="0" AllowsTransparency="False" StaysOpen="True">

        <Border BorderThickness="1" BorderBrush="Black" Background="AntiqueWhite">
            <WebBrowser x:Name="PopBrowser" local:WebBrowserUtility.Body="{Binding Path=Value}" Height="400" Width="500"/>
        </Border>
    </Popup>

尝试后面的代码,我添加了一个计时器,允许鼠标从按钮弹出(即使我没有偏移),然后添加一个If语句,以便在弹出时保持打开状态。

    Popup popUp { get; set; }

    private void ButtonMouseEnter(object sender, MouseEventArgs e)
    {
        Button currentButton = sender as Button;
        popUp = (Popup)FindResource("CustomPopup");
        DelayedExecutionService.DelayedExecute(() =>
        {
            if (currentButton.IsMouseOver)
            {
                popUp.PlacementTarget = currentButton;
                popUp.Placement = PlacementMode.Right;
                popUp.HorizontalOffset = -5;
                popUp.StaysOpen = true;
                popUp.IsOpen = true;
            }

        });
    }

    private void MouseLeave(object sender, MouseEventArgs e)
    {
        DelayedExecutionService.DelayedExecute(() =>
        {
            if (popUp.IsMouseDirectlyOver)
            {
                popUp.IsOpen = true;
                MessageBox.Show("over popup");  //Not recognizing this.
            }
            else
                popUp.IsOpen = false;
                MessageBox.Show("not over popup"); //I get this even if mouse is over popup.
        });
    }

此GIF仅针对弹出窗口显示MouseLeave,这对我的按钮网格非常不方便:

enter image description here

1 个答案:

答案 0 :(得分:0)

<Grid>
    <Button x:Name="Btn" MouseEnter="Button_MouseEnter_1" Content="Button" HorizontalAlignment="Left" Margin="32,41,0,0" VerticalAlignment="Top" Width="75"/>
    <Popup x:Name="Popup1" MouseLeave="Popup_MouseLeave_1" PlacementTarget="{Binding ElementName=Btn}" Placement="Bottom">
        <StackPanel>
            <TextBlock Text="Some popup text !"/>
            <Button Content="Close"/>
        </StackPanel>
    </Popup>
</Grid>

概念代码:

        private void Popup_MouseLeave_1(object sender, MouseEventArgs e)
        {
            Popup1.IsOpen = false;
        }

        private void Button_MouseEnter_1(object sender, MouseEventArgs e)
        {
            Popup1.IsOpen = true;
        }

告诉我这是否有用。