C# 每当鼠标在silverlight中离开时,关闭子窗口

C# 每当鼠标在silverlight中离开时,关闭子窗口,c#,silverlight,C#,Silverlight,当鼠标在银色的光线下从窗口离开时,想关上儿童窗口。就像面书一样 当鼠标悬停在父窗口超链接上时,我可以显示子窗口,但当鼠标离开时,我无法关闭子窗口。以上是我编写的代码片段 您必须在父窗口上调用以下事件处理程序: private void myChildWindow_MouseLeave(object sender, MouseEventArgs e) { this.close(); } 希望这对您有所帮助。ChildWindow控件有一个覆盖层,用于填充Silve

当鼠标在银色的光线下从窗口离开时,想关上儿童窗口。就像面书一样


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

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

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

希望这对您有所帮助。

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

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

有关ChildWindow控件的详细信息可在此处找到:

在父窗口上,尝试实现Leave Event HandlerParent to Child,而不是Child to Parent。您正在父窗口中写入子窗口名称,这是如何实现的??如果您有:class ChildWindow:ParentWindow意味着从父窗口继承,那么当然这是不可能的。我认为您的ParentWindow包含“myChildWindow”作为一个通过合成的对象。是否确实需要从父窗口继承?或者在我看来,构图似乎是一个更好的选择!!。
    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();
    }