1

I'm stuck with the closing behavior of a Popup. Have been searching the forum but couldn't find an answer that suits my case. I have a ListView and one Popup for the ListView. The Popup should be opened when a ListViewItem is clicked and should be closed when something else (other than a ListViewItem) is clicked. I'm working with MVVM so I have bound the IsOpen-Property of the Popup to a property in my VM which is set within the property bound to the SelectedItemProperty of the ListView. The code looks like that: MainWindow.xaml

<Grid>
    <ListView Name="List" ItemsSource="{Binding MyList}" SelectedItem="{Binding MyItem}" HorizontalAlignment="Left" />
     <Popup IsOpen="{Binding PopupOpen}" Placement="Right" StaysOpen="False" PlacementTarget="{Binding ElementName=List}">
        <TextBlock Text="I'm a Popup" />
    </Popup>
</Grid>

The code in my VM looks like that:

public class MyVM
{
    private string myItem;
    private bool popupOpen;

    public MyVM()
    {
        this.MyList = new List<string> { "Item 1", "Item 2", "Item 3" };            
    }

    public List<string> MyList { get; set; }

    public bool PopupOpen
    {
        get
        {
            return this.popupOpen;
        }
        set
        {
            this.popupOpen = value;
            this.OnPropertyChanged();
        }
    }

    public string MyItem
    {
        get
        {
            return this.myItem;
        }

        set
        {
            this.myItem = value;
            this.OnPropertyChanged();

            if (value != null)
            {
                this.PopupOpen = true;
            }
        }
    }
}

That's all. Now when I run this sample application the Popup opens as expected but only closes when the whole window loses its focus. But it should also close when I click somewhere outside of the ListView.

Any ideas?

CrazyChief
  • 197
  • 8
  • I don't see any execution path in your code where `PopupOpen` is set to `false`. – Moti Azu Dec 15 '14 at 10:08
  • it should be automatically closing. MSDN: When StaysOpen is false, the Popup control intercepts all mouse and keyboard events to determine when one of these events occurs outside the Popup control. – deafjeff Dec 15 '14 at 10:17
  • @CrazyChief You have something mixed up: public class MyVM, Public MainWindow() (looks like wrong constructor) – deafjeff Dec 15 '14 at 10:23
  • @Moti: PoupOpen is set to false by default. – CrazyChief Dec 15 '14 at 13:07
  • @deafjeff: It should but it doesn't ;) Copy and paste my code and try it out. Regarding the constructor: Updated my code sample above. Was a copy and paste error. It's correct in my project. – CrazyChief Dec 15 '14 at 13:09
  • If PopupOpen is set to true once (when an item gets selected) it will stay like that forever because you never change it back to false. This might override the behavior of StaysOpen. – Moti Azu Dec 16 '14 at 07:56
  • @MotiAzu No it doesn't. That's the point about StaysOpen="False". It will set "IsOpen" to false as soon as you click somewhere outside of the Popup. You can test that behavior if you move the line "this.PopupOpen = true" into the constructor and remove it from the setter of the MyItem-Property. If you do that everything works as expected. – CrazyChief Dec 16 '14 at 08:24
  • You could use Mouse.PreviewMouseDownOutsideCapturedElement(), and solve the not-closing problem. This is more reliable than dealing with the Popup's properties. See [here](http://stackoverflow.com/questions/6761786/how-can-a-control-handle-a-mouse-click-outside-of-that-control). This requires also capturing the mouse. – deafjeff Dec 16 '14 at 09:22

1 Answers1

0

Well I tried everything that came up my mind like MouseCapturing, playing with focus of elements inside of the Popup, opening the Popup from all kind of events (like the PreviewMouseDown, PreviewMouseUp of the ListItem) but couldn't get the StaysOpen functionality work properly. I'm out of ideas so I implemented the desired StaysOpen functionality myself:

To do that I registered the PreviewMouseLeftButtonDown of the MainWindow and handle the closing of the popup in there, i.e. close all Popups when a click on the Window occurs. To avoid the Popups being closed when you click inside of a Popup i added the same handler to the Popup and set the "IsOpen"-Property back to "true". Ugly but it works.

So if anyone has a better idea or can enlighten me about the internal functionality of how the StaysOpen-Property really works, I'd be very glad ;)

CrazyChief
  • 197
  • 8