每当老鼠离开在silverlight时关闭儿童窗口

时间:2014-01-21 07:05:11

标签: c# silverlight

private void myChildWindow_MouseLeave(object sender, MouseEventArgs e)
    {
        this.close();
    }

当老鼠从银色的灯光窗口离开时,想要关闭子窗口。就像面书一样。enter image description here

当鼠标悬停在父窗口超链接上时,我能够显示子窗口,但是当鼠标离开时我无法关闭子窗口。以上是我编写的代码片段。

2 个答案:

答案 0 :(得分:0)

您必须在父窗口上调用以下事件处理程序:

    private void ParentWindow_MouseLeave(object sender, EventArgs e)
    {
        myChildWindow.Close();
    }

希望这会有所帮助..

答案 1 :(得分:0)

ChildWindow控件有一个Overlay,用于填充Silverlight应用程序的整个可用区域。因此,在鼠标离开叠加层之前,子窗口上的鼠标离开事件不会触发。您需要将鼠标离开事件放在子窗口内容的根布局容器上。这是一个例子:

    private void ShowButton_Click(object sender, RoutedEventArgs e)
    {
        ChildWindow cw = new ChildWindow();
        cw.Width = 300;
        cw.Height = 300;
        cw.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Stretch;
        cw.VerticalContentAlignment = System.Windows.VerticalAlignment.Stretch;
        Grid g = new Grid();
        g.Background = new SolidColorBrush(Colors.Gray);
        g.Children.Add(new TextBlock() { Text = "Child window content." });
        g.MouseLeave += ChildWindowContent_MouseLeave;
        cw.Content = g;
        cw.Show();
    }

    private void ChildWindowContent_MouseLeave(object sender, MouseEventArgs e)
    {
        ChildWindow cw = ((Grid)sender).Parent as ChildWindow;
        cw.Close();
    }

有关ChildWindow控件的更多信息,请访问:http://msdn.microsoft.com/en-us/library/system.windows.controls.childwindow(v=vs.95).aspx