【问题标题】:How to detect the Popup LostFocus when only clicked outside the Popup and not to make the popup hidden仅在弹出窗口外部单击而不使弹出窗口隐藏时如何检测弹出窗口 LostFocus
【发布时间】: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 并且我可以让我的代码关闭弹出窗口还是保持打开状态(切换选项卡时)? 我还发现在启用上述属性时,一些双击事件不起作用,我不知道为什么。因此,我们将不胜感激。

【问题讨论】:

    标签: c# uwp popup


    【解决方案1】:

    当您使用属性IsLightDismissEnabled 时,它总是会关闭Popup,如果您愿意保留自定义控件的功能,我认为您不应该使用此属性。相反,您应该手动处理打开和关闭。

    我认为与其尝试处理LostFocus 事件,不如尝试OpenedClosed 事件,并利用可写属性IsOpen 手动打开和关闭Popup

    希望有帮助

    【讨论】:

      猜你喜欢
      • 2022-12-31
      • 2023-02-09
      • 2012-12-21
      • 1970-01-01
      • 2018-07-21
      • 2015-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多