microsoft-ui-xaml
microsoft-ui-xaml copied to clipboard
VisualTreeHelper unable to traverse TeachingTip content
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"/>
- 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>());
}
}
- Print out the visual tree in both
LoadedandLayoutUpdatedevent
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
Hi @HO-COOH , can you please provide a minimal repro project to investigate the issue further?
@snigdha011997 The original title is a bit misleading. I updated it and added a full repro link.