使用 StaysOpen 时 WPF 弹出窗口立即关闭

WPF Popup is closing immediately when using StaysOpen

我定义了一个弹出窗口:

<Popup StaysOpen="False" IsOpen="{Binding IsOpen, Mode=TwoWay}" />

我在视图模型中将 IsOpen 属性 的弹出窗口绑定到 IsOpen 属性。

将其设置为 true 会按预期工作并显示弹出窗口(当我在调试时单步执行代码时)。我在按钮的命令事件处理程序中将其设置为 true,如下所示:

<Button Command="{Binding OpenPopupCommand}" />

命令仅执行以下代码(在我的视图模型中):

IsOpen = true;

现在奇怪的是:

打开弹出窗口后,IsOpen 属性 setter 上的断点被值 false 击中,这导致弹出窗口立即关闭。

如果我删除 StaysOpen="False" 就不会发生这种情况(然后它默认为 True)。

好像弹出窗口正在显示,但它没有 get/looses 聚焦并关闭。

但我不明白为什么?

Popup.StaysOpen 属性:

Gets or sets a value that indicates whether the Popup control closes when the control is no longer in focus.

true if the Popup control closes when IsOpen property is set to false; false if the Popup control closes when a mouse or keyboard event occurs outside the Popup control. The default is true.

当您将此值设置为 false 时,一旦“在 Popup 控件外发生鼠标或键盘事件”,它将关闭。对于人类做出的微小的、不可避免的鼠标移动,您应该立即得到一个“MouseMoved”事件,除非鼠标出现时恰好位于弹出窗口上方。

当您将 StaysOpen 设置为 false 时,IsOpen 的值也会被忽略。