Get child element from a Window Handle in WinUI3

51 Views Asked by At

I am trying to find child elements from a parent Window Handle. I have a WinUI3 project which has button in stackpanel. The Sample code is given like this,

In MainWindow.xaml

<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
    <Button x:Name="myButton" Click="myButton_Click">Click Me</Button>
</StackPanel>

And in MainWindow.xaml.cs

private void myButton_Click(object sender, RoutedEventArgs e)
{
    myButton.Content = "Clicked";

    //IntPtr hWnd = new WindowInteropHelper(Application.Current.).EnsureHandle();

    var window = (Application.Current as App)?.Window as MainWindow;
    var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
}

But the problem is I could not find anyway to get element reference for myButton from hWnd.

0

There are 0 best solutions below