简体   繁体   English

如何仅在Popup外部单击时检测Popup LostFocus,而不是隐藏弹出窗口

[英]How to detect the Popup LostFocus when only clicked outside the Popup and not to make the popup hidden

I have a custom UWP Control where i use Popup in it and how can i detect the Popup Lost Focus? 我有一个自定义UWP控件,我在其中使用Popup,我如何检测Popup Lost Focus?

I have several tabs in my control and when i click any of my tab, the popup should be visible and vise versa and i have states for my tab to indicate the popup state. 我的控件中有几个选项卡,当我单击任何选项卡时,弹出窗口应该是可见的,反之亦然,我的选项卡状态指示弹出状态。

Now i need to close the Popup when clicked outside the popup. 现在我需要在弹出窗口外单击时关闭弹出窗口。 I have done my research and found a property IsLightDismissEnabled which does close the Popup automatically and invokes LostFocus. 我做了我的研究,发现了一个属性IsLightDismissEnabled ,它自动关闭Popup并调用LostFocus。 But when switching between the Tabs the popup should not be closed , but when i use this property the popup is automatically closing which is not i desired. 但是当在Tabs之间切换时,不应该关闭弹出窗口,但是当我使用这个属性时,弹出窗口会自动关闭,这不是我想要的。

This is the code i have In the code im updating the state based on whether Popup is already open and whether new tab is selected. 这是我的代码在代码中我根据Popup是否已经打开以及是否选择了新选项卡来更新状态。 And in State property im updating the Popup visibility 并在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;
                }
            }
        }

Is there any way to invoke only LostFocus and i can have my code whether to close the Popup or stay it open(When Switching Tabs) ?? 有没有办法只调用LostFocus,我可以让我的代码是关闭弹出窗口还是保持打开(切换选项卡时)? I also found when enabling the mentioned property, some double tap events were not working and i dont know why . 我也发现当启用上述属性时,一些双击事件不起作用,我不知道为什么。 So any help is appreciated . 所以任何帮助都表示赞赏。

When you use the property IsLightDismissEnabled , it will always close the Popup and I don't think you shouldn't use this property if you are willing to keep the functionality of your custom control. 当您使用属性IsLightDismissEnabled ,它将始终关闭Popup IsLightDismissEnabled ,如果您愿意保留自定义控件的功能,我认为您不应该使用此属性。 Rather you should handle the opening and closing manually. 相反,您应该手动处理打开和关闭。

I think instead of trying to handle the LostFocus event, You should try the Opened and Closed event instead and utilize the writable property IsOpen to manually open and close the Popup . 我认为不应该尝试处理LostFocus事件,而应该尝试使用OpenedClosed事件,并利用可写属性IsOpen手动打开和关闭Popup

Hope that helps 希望有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM