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?