鼠标单击弹出窗口的外框时如何防止弹出窗口关闭

时间:2019-04-08 11:11:23

标签: c# wpf

在WPF中,当我们单击“切换”按钮然后弹出窗口正常打开时,但是当我们在弹出窗口之外单击时弹出窗口被自动关闭。如何防止这种情况。

当我们在弹出窗口之外单击时,不应关闭弹出窗口。

代码:

<ToggleButton Name="TglBtn" Content="Document" /> 
<Popup IsOpen="{Binding IsChecked, ElementName=TglBtn}" StaysOpen="False"   PlacementTarget="{Binding ElementName=popupDocshow}" x:Name="pop1"> 
  <TextBlock Text="Documents" Background="Blue"/> 
</Popup>

1 个答案:

答案 0 :(得分:1)

PopupTest.StaysOpen = true这就是您所缺少的。

<Popup x:Name="PopupTest" AllowsTransparency="True">
    <Viewbox VerticalAlignment="Top">
        <TextBlock Text="Wow, that was easy!"/>
    </Viewbox>
</Popup>

private void Button_Click(object sender, RoutedEventArgs e)
{
    PopupTest.Placement = System.Windows.Controls.Primitives.PlacementMode.Mouse;
    PopupTest.StaysOpen = true;
    PopupTest.Height = 1000;
    PopupTest.Width = 500;
    PopupTest.IsOpen = true;       
}

这是我选中的弹出窗口