In c++ how can I access a XAML FrameworkElement from a separate class?


  • In c++ how can I access a XAML FrameworkElement on a page from a separate class? 
    Or at least be able to pass them one way somehow to a class.

    SB Software Developer

    Monday, October 1, 2012 8:23 AM


  • Hello,

    I think you can try to traversal visual tree with the name of that XAML control.
    You can try this function.

    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)
                return ret;

    Best regards,

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

    • Marked as answer by Jesse Jiang Monday, October 8, 2012 3:11 AM
    Tuesday, October 2, 2012 3:20 AM