What's the way to find the Window that references UserControl? #15765
-
There is a button in UserControl, and I need to use ShowDialog to open a window when the button is clicked, but I haven't found a good way.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You can use the TopLevel APIs. https://docs.avaloniaui.net/docs/concepts/toplevel EG var window = TopLevel.GetTopLevel(this) as Window; Windows themselves inherit from TopLevel But you should probably still check if the top level is actually a window as there are a few things that implement it which you may run into depending on how your app runs. That said I have never seen it return anything but a Window when running a desktop app. Not sure what it returns on non desktop apps. |
Beta Was this translation helpful? Give feedback.
You can use the TopLevel APIs.
https://docs.avaloniaui.net/docs/concepts/toplevel
EG
Windows themselves inherit from TopLevel
Avalonia/src/Avalonia.Controls/WindowBase.cs
Line 18 in 42aa77e
But you should probably still check if the top level is actually a window as there are a few things that implement it which you may run into depending on how your app runs. That said I have never seen it return anything but a Window when running a desktop app. Not sure what it returns on non desktop apps.