views:

115

answers:

2

Hi,

On my ListBoxItem MouseEnter event I am creating a new window with the following code.

Window w = new Window();
w.Show();

When the mouse leaves the current item I want to close the window.

How do I do that?

Many Thanks.

+2  A: 

Store a reference to the window (w in your case) in a place where both the event handler for MouseEnter and MouseExit can access it and then just do a w.Close().

Joey
Hi, I have been thinking on the same line, but how do I store a reference?
Anand
@Anand: Usually you do this with a field in the class.
John Feminella
A: 

Instead of storing the w in a local variable, store it in a member variable of the current class. On MouseExit, use the member variable to close the Window.

public partial class ListWindow
{
    Window displayWindow;

    public void OnListBoxItem_MouseEnter()
    {
        displayWindow = new Window();
        displayWindow.Show();
    }

    public void OnListBoxItem_MouseExit()
    {
        displayWindow.Close();
    }
}
epotter