Insert Visual at the bottom of a WinUI 3 custom control

Alvaro Rivoir 0 Reputation points
2024-08-27T17:55:06.9933333+00:00

I'm trying to create a custom xaml control in WinUI3 and found the Microsoft.UI.Xaml.Hosting.ElementCompositionPreview has some method to manipulate the visual composition tree, but I need the element to be a mix of elements in the ControlTemplate and some visual elements added by the custom control.
So far I was able to get the visual of the control by using

var visual = Microsoft.UI.Xaml.Hosting.ElementCompositionPreview.GetElementVisual(this);


And I could create the visual I need to insert, but it always appear in front of the ControlTemplate elements
I tried using

Microsoft.UI.Xaml.Hosting.ElementCompositionPreview.SetElementChildVisual(this, _drawingVisual);

and

visual.Children.InsertAtBottom(_drawingVisual);

And both are presented on top of the control template. I need them at the background

Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,274 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.