Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

WinRT/C++ Get parent Page control of the Page inside the Frame

Best way to get parent Page control from Page inside Frame on desktop application built with WinRT/C++ UI.

Illustration:

MainPage
    ^^Frame
        ^^SecondPage
            ^^Frame
                ^^ThirdPage
                    ^^Keep going

So, how to get MainPage control from SecondPage or even from ThirdPage onwards.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use the VisualTreeHelper to navigate the visual tree. The following implementation goes up the visual tree starting at a root element, and returns an element that matches the requested ancestor_type, or a null com_ptr if there isn’t any that matches:

template <typename ancestor_type>
auto find_ancestor(::winrt::Windows::UI::Xaml::DependencyObject root) noexcept
{
    auto ancestor { root.try_as<ancestor_type>() };
    while (!ancestor && root)
    {
        root = ::winrt::Windows::UI::Xaml::Media::VisualTreeHelper::GetParent(root);
        ancestor = root.try_as<ancestor_type>();
    }
    return ancestor;
}

You can use this to move from Page to Page, skipping over the intermediate Frame‘s until you are at the top of the tree.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading