locked
How to use FindName to find specific button?

    Question

  • Hi!

    I'm trying to change the background on a specific button that is created dynamically. I suppose that FindName is best way to do this, but my code will not work. Can someone help me?

    This is my code for adding the buttons to StackPanel named 'shortcut'. 

                        Button btn = new Button();
                        btn.Name = "color" + i;
                        RegisterName(btn.Name, btn);
                        btn.Content = colorlist[i];
                        btn.Click += btn_Clicked;
                        shortcut.Children.Add(btn);
    

    I try to change the background color in the method named FlipView_SelectionChanged. Don't know what to do with the method RegisterName.

           private void RegisterName(string p, Button btn)
            {
                throw new NotImplementedException();
            }
    
    
            private void FlipView_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                int i = flipView.SelectedIndex;
                //Button btn = new Button();
                Button btn = (Button)shortcut.FindName("color" + i);
                if(btn != null)
                {
                    btn.Background = new SolidColorBrush(Windows.UI.Colors.Red);
                }
            }

    Thanks, Sigurd F

    Friday, August 29, 2014 10:53 PM

Answers

  • There is no RegisterName method available in Windows Store Apps and you should not use the FindName method to find dynamically added controls. What you should do is to look for the dynamically added control in the visual tree. You could use the following recursive help method to do this:

           
    private void FlipView_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                int i = flipView.SelectedIndex;
                Button btn = FindVisualChild<Button>(shortcut, "color" + i);
                if (btn != null)
                {
                    btn.Background = new SolidColorBrush(Windows.UI.Colors.Red);
                }
            }
    
    private static T FindVisualChild<T>(DependencyObject parent, string childName) where T : DependencyObject
            {
                if (parent == null)
                    return null;
    
                T foundChild = null;
    
                int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
                for (int i = 0; i < childrenCount; i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(parent, i);
                    T childType = child as T;
                    if (childType == null)
                    {
                        // recursively drill down the tree
                        foundChild = FindVisualChild<T>(child, childName);
                        if (foundChild != null) 
                            break;
                    }
                    else if (!string.IsNullOrEmpty(childName))
                    {
                        FrameworkElement frameworkElement = child as FrameworkElement;
                        if (frameworkElement != null && frameworkElement.Name == childName)
                        {
                            foundChild = (T)child;
                            break;
                        }
                    }
                    else
                    {
                        foundChild = (T)child;
                        break;
                    }
                }
    
                return foundChild;
            }
    

    Please remember to mark helpful posts as answer and/or helpful.

    • Marked as answer by Sigurd F Saturday, August 30, 2014 7:08 PM
    Saturday, August 30, 2014 8:49 AM

All replies

  • There is no RegisterName method available in Windows Store Apps and you should not use the FindName method to find dynamically added controls. What you should do is to look for the dynamically added control in the visual tree. You could use the following recursive help method to do this:

           
    private void FlipView_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                int i = flipView.SelectedIndex;
                Button btn = FindVisualChild<Button>(shortcut, "color" + i);
                if (btn != null)
                {
                    btn.Background = new SolidColorBrush(Windows.UI.Colors.Red);
                }
            }
    
    private static T FindVisualChild<T>(DependencyObject parent, string childName) where T : DependencyObject
            {
                if (parent == null)
                    return null;
    
                T foundChild = null;
    
                int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
                for (int i = 0; i < childrenCount; i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(parent, i);
                    T childType = child as T;
                    if (childType == null)
                    {
                        // recursively drill down the tree
                        foundChild = FindVisualChild<T>(child, childName);
                        if (foundChild != null) 
                            break;
                    }
                    else if (!string.IsNullOrEmpty(childName))
                    {
                        FrameworkElement frameworkElement = child as FrameworkElement;
                        if (frameworkElement != null && frameworkElement.Name == childName)
                        {
                            foundChild = (T)child;
                            break;
                        }
                    }
                    else
                    {
                        foundChild = (T)child;
                        break;
                    }
                }
    
                return foundChild;
            }
    

    Please remember to mark helpful posts as answer and/or helpful.

    • Marked as answer by Sigurd F Saturday, August 30, 2014 7:08 PM
    Saturday, August 30, 2014 8:49 AM
  • Thanks a lot. This made my day :-)

    Regards, Sigurd F

    Saturday, August 30, 2014 7:07 PM