locked
How to get named elements in controltemplate?

    Question

  • I found there were no findname() in controltemplate, I am developing windows 8 metro C++ App.

    Thursday, October 11, 2012 2:31 AM

Answers

  • Hi,

    You can use this function to traversal visual tree by name

    Windows::UI::Xaml::FrameworkElement^ FindVisualChildByName(DependencyObject^ obj, String^ name)
    {
    	FrameworkElement^ ret ;
    	int numChildren = VisualTreeHelper::GetChildrenCount(obj);
    	 for (int i = 0; i < numChildren; i++)
                {
                    auto objChild = VisualTreeHelper::GetChild(obj, i);
                    auto child = safe_cast<FrameworkElement^>(objChild);
    				if (child != nullptr && child->Name == name)
                    {
                        return child;
                    }
    
                    ret = FindVisualChildByName(objChild, name);
    				if (ret != nullptr)
                        break;
                }
                return ret;
    }
    

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by WillNo1 Monday, October 15, 2012 9:28 AM
    Friday, October 12, 2012 7:07 AM