question

zequion-6374 avatar image
0 Votes"
zequion-6374 asked ·

Not all controls are retrieved from an .aspx page

I use a recursive function to locate all the controls on a .aspx page in WPF and in Asp.net, but many controls are not localized.If I use control.FindName ("Control_Name") it does find those controls.

I need the recursive function to find all the controls because I can't go searching one by one by name.
In this specific case of the example, dxdo are devexpress controls that contain normal controls like <grid> that are not localized.


Example: Controls below AutoHideGroup are not found (and Form_OpcionesEx2_Panel is visible):

                     <dxdo:AutoHideGroup x:Name="Form_OpcionesEx2_AutoHideGroup">
                         <dxdo:LayoutPanel x:Name="Form_OpcionesEx2_Panel" Caption="Details" Visibility="Visible">

My Recursive code (Resume):

internal static dynamic Fcn_ControlFindRec(dynamic ThRead_Funciones, ref dynamic ThRead_Controles, System.Windows.DependencyObject MyContenedor, string MyControlNombre
{ dynamic MyDynRet = null;

         try
         {   int MyChildrenCount = System.Windows.Media.VisualTreeHelper.GetChildrenCount(MyContenedor);

             for (int index = 0; index < MyChildrenCount; index++)
             {   System.Windows.DependencyObject MyChild = System.Windows.Media.VisualTreeHelper.GetChild(MyContenedor, index);
                    
                 MyDynRet = Name_Wpf_Control_Find.Cls_Wpf_Control_Find.Fcn_ControlFindRec(ThRead_Funciones, ref ThRead_Controles, MyChild, MyControlNombre);
             }
         }
         catch (System.Exception ErrorExcp){}
         return MyDynRet;
     }



windows-wpf
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

AlexLi-MSFT avatar image
0 Votes"
AlexLi-MSFT answered ·

Welcome to our Microsoft Q&A platform!

Is the project WPF or Asp.net?If it is wpf and you want to get child control by name,you can use the following code:

 public T GetChildObject<T>(DependencyObject obj, string name) where T : FrameworkElement
           {
               DependencyObject child = null;
               T grandChild = null;
               for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
               {
                  child = VisualTreeHelper.GetChild(obj, i);
                  if (child is T && (((T)child).Name == name || string.IsNullOrEmpty(name)))
                  {
                      return (T)child;
                  }
                  else
                  {
                      grandChild = GetChildObject<T>(child, name);
                      if (grandChild != null)
                      return grandChild;
                  }
              }
              return null;
          }

Thanks.

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.