当从组合框中选择一个项目时,我需要打开一个弹出窗口。 这部分工作正常。但是,单击外部时弹出窗口不会关闭。 仅当单击弹出窗口内的按钮然后在外部单击时,它才会关闭。 我试过设置staysopen = false,并尝试将焦点放在按钮上。 XAML
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ComboBox Name="cmb" ItemsSource="abcdef" SelectionChanged="ComboBox_SelectionChanged" Margin="0,0,0,232"></ComboBox>
<Popup Name="p1" StaysOpen="False" Margin="12,85,12,159" PlacementTarget="{Binding ElementName=cmb}">
<Grid>
<Button Name="btn1" Height="100" Width="100" Content="i am in a popup"/>
</Grid>
</Popup>
</Grid>
</Window>
代码背后
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
p1.IsOpen = true;
}
}
}