C#WPF Pop被多次绘制

时间:2016-06-23 21:37:15

标签: c# wpf xaml

我创建了一个用于查看特定文件类型的自定义十六进制查看器工具。 作为要求的一部分,一旦我将鼠标悬停在十六进制范围内(通过C#Run类实现),我需要突出显示某些位值。

问题是大约有50%的时间我会将多个弹出窗口相互叠加而不是一个弹出窗口。

见下文: enter image description here

这是我在C#中的相关代码段:

private Popup popup = new Popup();

        void ToolTip_MouseEnter(object sender, EventArgs e)
        {
            //TODO: base popup action on data value
            if (popup.IsOpen!=true)
            {
                if (sender is Run && HexDocumentUIHelperUtility.zftSequenceBitsMouseUp)
                {
                    Run runControl = sender as Run;
                    if (runControl != null)
                    {


                        //popup.HorizontalAlignment = HorizontalAlignment.Center;
                        //popup.VerticalAlignment = VerticalAlignment.Center;

                        TextBox textBox = new TextBox(); 
                        textBox.Text = this.getZftBitsVisualization().getBinaryString();
                        int startHighlight = this.getZftBitsVisualization().getHighlightIndex();
                        int length = this.getZftBitsVisualization().getHighlightLength();
                        //textBox.SelectionStart = startHighlight;
                        //textBox.SelectionLength = length;
                        textBox.SelectionBrush = Brushes.Gold;
                        textBox.Select(startHighlight, length);
                        textBox.FontSize = 15; 
                        popup.Child = textBox;
                        //get the current mouse position 
                        //I adjusted the mouse Y coordinate by minus 20 pixels in order to avoid the popup vbeing displayed on top of the hex range
                        int mouseYCoordinate = System.Windows.Forms.Control.MousePosition.Y + 20;
                        popup.HorizontalOffset = System.Windows.Forms.Control.MousePosition.X;
                        popup.VerticalOffset = mouseYCoordinate;
                        popup.IsOpen = true;
                        textBox.Focus();

                    }
                }
            }//if the pop is not already opened 
        }

        void ToolTip_MouseLeave(object sender, EventArgs e)
        {
            if (sender is Run)
            {
                Run runControl = sender as Run;
                if (runControl != null)
                {
                    if (popup != null)
                    {
                        popup.IsOpen = false;
                        popup.Child = null;


                    }
                    if (highlightedRunList != null)
                    {
                        highlightedRunList.Clear();
                    }

                }
            }
        }

1 个答案:

答案 0 :(得分:1)

您正在测试弹出窗口是否已在方法顶部打开,但实际上并未将其设置为直至更远的位置。

这使得鼠标在最终将IsOpen设置为true之前会多次触发事件,以防止进一步弹出窗口。

在测试后立即将IsOpen的设置移动到弹出窗口未打开。如果弹出窗口失败,您始终可以将其设置为false。