【发布时间】:2019-05-02 23:07:29
【问题描述】:
我有一个自定义 UWP 控件,我在其中使用 Popup,我如何检测 Popup Lost Focus?
我的控件中有几个选项卡,当我单击任何选项卡时,弹出窗口应该可见,反之亦然,并且我的选项卡有状态来指示弹出状态。
现在我需要在弹出窗口外单击时关闭弹出窗口。我进行了研究,发现了一个属性 IsLightDismissEnabled,它会自动关闭 Popup 并调用 LostFocus。但是在选项卡之间切换时,不应关闭弹出窗口,但是当我使用此属性时,弹出窗口会自动关闭,这不是我想要的。
这是我的代码 在代码中,我根据 Popup 是否已打开以及是否选择了新选项卡来更新状态。 并在 State 属性中即时更新 Popup 可见性
if (this.PART_Popup != null)
{
this.PART_Popup.LostFocus += this.PART_Popup_LostFocus;
this.PART_Popup.IsLightDismissEnabled = true;
}
private void PART_Popup_LostFocus(object sender, RoutedEventArgs e)
{
if (this.ParentItemsControl != null)
{
if (this.ParentItemsControl.State == State.Adorner)
{
if (this.ParentItemsControl.PART_Popup.IsOpen && this.ParentItemsControl.SelectedItem == this)
{
this.ParentItemsControl.State = State.Hide;
}
}
else
{
this.ParentItemsControl.State = State.Normal;
}
}
}
有什么方法可以只调用 LostFocus 并且我可以让我的代码关闭弹出窗口还是保持打开状态(切换选项卡时)? 我还发现在启用上述属性时,一些双击事件不起作用,我不知道为什么。因此,我们将不胜感激。
【问题讨论】: