Skip to content

VisualTreeHelper unable to traverse TeachingTip content #10857

@HO-COOH

Description

@HO-COOH

Describe the bug

I am doing some customization using attached DependencyProperty, so I attached a Loaded event handler to a TeachingTip to traverse it with VisualTreeHelper, but it is unable to traverse the visual tree of it, giving no useful information.

Why is this important?

This should work

Steps to reproduce the bug

I have a TeachingTip defined in xaml

        <Button x:Name="Button" Click="Button_Click">Click to open tip</Button>
        <TeachingTip
            x:Name="MyTip"
            Loaded="MyTip_Loaded"
            LayoutUpdated="MyTip_LayoutUpdated"
            Content="Some tip"/>
  1. Add a helper function to traverse a control and print it type name and name
static void printVisualTree(winrt::Microsoft::UI::Xaml::FrameworkElement const& element)
{
	if (!element)
		return;

	OutputDebugString(std::format(L"{}: {}\n", winrt::get_class_name(element), element.Name()).data());

	auto const numChild = winrt::Microsoft::UI::Xaml::Media::VisualTreeHelper::GetChildrenCount(element);
	for (auto i = 0; i < numChild; ++i)
	{
		printVisualTree(winrt::Microsoft::UI::Xaml::Media::VisualTreeHelper::GetChild(element, i).as<winrt::Microsoft::UI::Xaml::FrameworkElement>());
	}
}
  1. Print out the visual tree in both Loaded and LayoutUpdated event
	void MainWindow::MyTip_Loaded(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e)
	{
		auto teachingTip = sender.as<winrt::Microsoft::UI::Xaml::Controls::TeachingTip>();
		OutputDebugString(L"==============Loaded==========\n");
		printVisualTree(teachingTip);
	}

	void MainWindow::MyTip_LayoutUpdated(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::Foundation::IInspectable const& e)
	{
		OutputDebugString(L"==============Layout Updated==========\n");
		printVisualTree(MyTip());
	}

Actual behavior

Gives

==============Layout Updated==========
Microsoft.UI.Xaml.Controls.TeachingTip: MyTip
Microsoft.UI.Xaml.Controls.Border: Container
==============Loaded==========
Microsoft.UI.Xaml.Controls.TeachingTip: MyTip
Microsoft.UI.Xaml.Controls.Border: Container

Expected behavior

No response

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 1.8.2: 1.8.251003001

Windows version

Windows 11 (24H2): Build 26100

Additional context

Full repro

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-TeachingTipbugSomething isn't workingneeds-triageIssue needs to be triaged by the area owners

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions