locked
how to access all contols on a page and select a few based upon certain condition and changing their properties.

    Question

  • on a page out of many controls like text boxes, text blocks, buttons i want all of these not enabled on page load event except a few selected one for instance a back button. i did it the following way.

    this.IsEnabled = false;
    this.backButton.IsEnabled = true;


    after disabling all controls it is not possible to enable a particular one.

    interestingly if i use it this way.

     this.backButton.IsEnabled = false;
     this.IsEnabled = true;


    in this case the back button does not get enabled again.

    please suggest me a way out it seems that isenabled one property once changed can not be changed again in the same event procedure.

    i also tried to search how to loop through all the controls but it didn't worked.

    • Edited by samul tyagi Friday, October 31, 2014 3:39 PM
    Friday, October 31, 2014 3:36 PM

Answers

  • Hi Samul,

    The elements in XAML are organized as a tree (a so called Visual Tree). In order to traverse the tree you need to use a recursive procedure.

    The following code disables all controls on the page except one provided as a parameter:

    DisableElements(this, "BackButtonName");
    
    // ...
    
    public void DisableElements(DependencyObject root, string doNotDisableControlName)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(root, i);
    
            if (child != null)
            {
                Control ctrl = child as Control;
                if (ctrl != null)
                {
                    if (ctrl.Name != doNotDisableControlName)
                        ctrl.IsEnabled = false;
                }
    
                if (VisualTreeHelper.GetChildrenCount(child) > 0)
                    DisableElements(child, doNotDisableControlName);
            }
        }
    }

    Leszek


    My Apps



    • Edited by ata6502 Friday, October 31, 2014 4:47 PM
    • Marked as answer by samul tyagi Friday, October 31, 2014 5:48 PM
    • Unmarked as answer by samul tyagi Saturday, November 01, 2014 9:59 AM
    • Marked as answer by samul tyagi Saturday, November 01, 2014 12:04 PM
    Friday, October 31, 2014 4:45 PM
  • finally i did it

    here is the corrected code

    public void EnableElements(DependencyObject root) 
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++) 
                {
                    DependencyObject child = VisualTreeHelper.GetChild(root,i);
                    if (child != null)
                    {
                        Control ctrl = child as Control;
                        if (ctrl != null)
                        {
                            ctrl.IsEnabled = true;
                        }
                        if (VisualTreeHelper.GetChildrenCount(child) > 0)
                            EnableElements(child);
                    }
                            
                            
                    
                }
            }

    • Marked as answer by samul tyagi Saturday, November 01, 2014 12:04 PM
    Saturday, November 01, 2014 12:04 PM

All replies

  • Hi Samul,

    The elements in XAML are organized as a tree (a so called Visual Tree). In order to traverse the tree you need to use a recursive procedure.

    The following code disables all controls on the page except one provided as a parameter:

    DisableElements(this, "BackButtonName");
    
    // ...
    
    public void DisableElements(DependencyObject root, string doNotDisableControlName)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(root, i);
    
            if (child != null)
            {
                Control ctrl = child as Control;
                if (ctrl != null)
                {
                    if (ctrl.Name != doNotDisableControlName)
                        ctrl.IsEnabled = false;
                }
    
                if (VisualTreeHelper.GetChildrenCount(child) > 0)
                    DisableElements(child, doNotDisableControlName);
            }
        }
    }

    Leszek


    My Apps



    • Edited by ata6502 Friday, October 31, 2014 4:47 PM
    • Marked as answer by samul tyagi Friday, October 31, 2014 5:48 PM
    • Unmarked as answer by samul tyagi Saturday, November 01, 2014 9:59 AM
    • Marked as answer by samul tyagi Saturday, November 01, 2014 12:04 PM
    Friday, October 31, 2014 4:45 PM
  • thanks i used it but after using it i am not able to enable back my controls.

    i used this.isenabled = true

    on click event of edit button so that a user can edit the data.

    i also tried to do enable by the way you showed me to disable the controls.

    so now also tell me how to solve this new problem coming. it should not come but i don't know why it is happening

    i debugged the code and in the event that i am using to enable controls no child is returned.

    you used the call and in that since grid being at i = 0 and having more child get disabled. so i used the same code to enable as shown.

     public void EnableElements(DependencyObject root) 
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++) 
                {
                    DependencyObject child = VisualTreeHelper.GetChild(root, i);
                   
                        Control ctrl = child as Control;
                        if (ctrl != null) 
                        {
                            ctrl.IsEnabled = true;
                            if (VisualTreeHelper.GetChildrenCount(child) > 0)
                                EnableElements(child);
                        }
                    
                }
            }


    the problem is
     DependencyObject child = VisualTreeHelper.GetChild(root, 0);
    even this is returning the child correctly then also i can't enable it



    • Edited by samul tyagi Saturday, November 01, 2014 11:18 AM
    Saturday, November 01, 2014 10:02 AM
  • finally i did it

    here is the corrected code

    public void EnableElements(DependencyObject root) 
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++) 
                {
                    DependencyObject child = VisualTreeHelper.GetChild(root,i);
                    if (child != null)
                    {
                        Control ctrl = child as Control;
                        if (ctrl != null)
                        {
                            ctrl.IsEnabled = true;
                        }
                        if (VisualTreeHelper.GetChildrenCount(child) > 0)
                            EnableElements(child);
                    }
                            
                            
                    
                }
            }

    • Marked as answer by samul tyagi Saturday, November 01, 2014 12:04 PM
    Saturday, November 01, 2014 12:04 PM