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

    Question

  • 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 01, 2012 8:23 AM

Answers

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

    Best regards,
    Jesse


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

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