在WPF应用程序中查找所有打开的Popup

时间:2022-10-02 23:03:35

WPF has the Popup class with which you can open a (small) window inside another window. This is used for example for Tooltips or ComboBoxes.

WPF具有Popup类,您可以使用该类在另一个窗口中打开(小)窗口。这用于例如Tooltips或ComboBoxes。

I need to find all of these popups which are currently opened inside a WPF window, so I can close them.

我需要找到当前在WPF窗口中打开的所有弹出窗口,以便我可以关闭它们。

2 个解决方案

#1


11  

If someone still need:

如果有人仍然需要:

  public static IEnumerable<Popup> GetOpenPopups()
  {
      return PresentationSource.CurrentSources.OfType<HwndSource>()
          .Select(h => h.RootVisual)
          .OfType<FrameworkElement>()
          .Select(f => f.Parent)
          .OfType<Popup>()
          .Where(p => p.IsOpen);
  }

#2


0  

One way of doing this would be to navigate the Visual Tree to find all Popup objects, and close them if they're open

这样做的一种方法是导航Visual Tree以查找所有Popup对象,并在它们打开时关闭它们

I have some VisualTreeHelpers on my blog that provide an example of how you can navigate the Visual Tree, although they are setup to only return a single object that matches the specified criteria, and not all objects.

我的博客上有一些VisualTreeHelpers提供了一个如何导航Visual Tree的示例,尽管它们被设置为仅返回与指定条件匹配的单个对象,而不是所有对象。

You'll probably need to modify the code a bit to make sure it returns all objects, but the code I use for returning a single object looks like this:

您可能需要稍微修改代码以确保它返回所有对象,但我用于返回单个对象的代码如下所示:

    /// <summary>
    /// Looks for a child control within a parent by type
    /// </summary>
    public static T FindChild<T>(DependencyObject parent)
        where T : DependencyObject
    {
        // Confirm parent is valid.
        if (parent == null) return null;

        T foundChild = null;

        int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            // If the child is not of the request child type child
            T childType = child as T;
            if (childType == null)
            {
                // recursively drill down the tree
                foundChild = FindChild<T>(child);

                // If the child is found, break so we do not overwrite the found child.
                if (foundChild != null) break;
            }
            else
            {
                // child element found.
                foundChild = (T)child;
                break;
            }
        }
        return foundChild;
    }   

And can be called like this:

并且可以像这样调用:

var popup = MyVisualTreeHelpers.FindChild<Popup>(MyWindow);

#1


11  

If someone still need:

如果有人仍然需要:

  public static IEnumerable<Popup> GetOpenPopups()
  {
      return PresentationSource.CurrentSources.OfType<HwndSource>()
          .Select(h => h.RootVisual)
          .OfType<FrameworkElement>()
          .Select(f => f.Parent)
          .OfType<Popup>()
          .Where(p => p.IsOpen);
  }

#2


0  

One way of doing this would be to navigate the Visual Tree to find all Popup objects, and close them if they're open

这样做的一种方法是导航Visual Tree以查找所有Popup对象,并在它们打开时关闭它们

I have some VisualTreeHelpers on my blog that provide an example of how you can navigate the Visual Tree, although they are setup to only return a single object that matches the specified criteria, and not all objects.

我的博客上有一些VisualTreeHelpers提供了一个如何导航Visual Tree的示例,尽管它们被设置为仅返回与指定条件匹配的单个对象,而不是所有对象。

You'll probably need to modify the code a bit to make sure it returns all objects, but the code I use for returning a single object looks like this:

您可能需要稍微修改代码以确保它返回所有对象,但我用于返回单个对象的代码如下所示:

    /// <summary>
    /// Looks for a child control within a parent by type
    /// </summary>
    public static T FindChild<T>(DependencyObject parent)
        where T : DependencyObject
    {
        // Confirm parent is valid.
        if (parent == null) return null;

        T foundChild = null;

        int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            // If the child is not of the request child type child
            T childType = child as T;
            if (childType == null)
            {
                // recursively drill down the tree
                foundChild = FindChild<T>(child);

                // If the child is found, break so we do not overwrite the found child.
                if (foundChild != null) break;
            }
            else
            {
                // child element found.
                foundChild = (T)child;
                break;
            }
        }
        return foundChild;
    }   

And can be called like this:

并且可以像这样调用:

var popup = MyVisualTreeHelpers.FindChild<Popup>(MyWindow);