WPF: How do I loop through the all controls in a window?

前端 未结 5 1017
无人及你
无人及你 2020-11-27 05:52

How do I loop through the all controls in a window in WPF?

5条回答
  •  醉话见心
    2020-11-27 06:38

    I found this in the MSDN documenation so it helps.

    // Enumerate all the descendants of the visual object.
    static public void EnumVisual(Visual myVisual)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(myVisual); i++)
        {
            // Retrieve child visual at specified index value.
            Visual childVisual = (Visual)VisualTreeHelper.GetChild(myVisual, i);
    
            // Do processing of the child visual object.
    
            // Enumerate children of the child visual object.
            EnumVisual(childVisual);
        }
    }
    

    Looks simpler to me. I used it to find textboxes in a form and clear their data.

提交回复
热议问题