I am using Caliburn.Micro for a WPF application.
One of the feature I am using wraps a view in the a popup control and let me choose the settings.
But I came to the conclusion that this is a behaviour I cannot solve.
In the code behind when I click the Button a popup appears and when I click elsewhere on the screen it goes away as expected.
When I click on an item in the list, the popup appears but does not disappear if I click elsewhere on the window. (it does though if i click somewhere else, like another application)
Basically I cannot create a PopUp with StaysOpen = false behaviour when handling a selected item event. What can i do to solve this?
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox Name="ListBox" Width="250" HorizontalAlignment="Left" SelectionChanged="ListBox_SelectionChanged">
</ListBox>
<Button Name="Button" HorizontalAlignment="Right" Content="ShowPopUp" Click="Button_Click" />
</Grid>
</Window>
and the code behind
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.ListBox.ItemsSource = new System.Collections.ObjectModel.ObservableCollection<MainWindowItem>()
{
new MainWindowItem {Name = "Test 001" },
new MainWindowItem {Name = "Test 002" },
new MainWindowItem {Name = "Test 003" },
};
}
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selection = this.ListBox.SelectedItem as MainWindowItem;
if (selection == null)
{
return;
}
var popUp = new System.Windows.Controls.Primitives.Popup();
popUp.Child = new TextBox { Text = selection.Name , MinWidth = 200 , MinHeight = 32};
popUp.StaysOpen = false;
popUp.AllowsTransparency = true;
popUp.PlacementTarget = sender as FrameworkElement;
popUp.IsOpen = true;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var popUp = new System.Windows.Controls.Primitives.Popup();
popUp.Child = new TextBox { Text = "This is button Clicked" , MinWidth = 200, MinHeight = 32 };
popUp.StaysOpen = false;
popUp.AllowsTransparency = true;
popUp.PlacementTarget = sender as FrameworkElement;
popUp.IsOpen = true;
}
}
public class MainWindowItem
{
public string Name { get; set; }
public string Value { get; set; }
}
}