简体   繁体   中英

Set Window owner inside of an UserControl in WPF

I have an UserControl that shows a window without an icon in the Taskbar, the problem is when the main window lost the focus and get it again the window without taskbar icon is behind the main window and is difficult show it in front again, this issue is solved using window.Owner = this; when the class is a window, but when is UserControl or other type (eg ListBoxItem , Viewbox ) I can't do this. I tried to do this control.Owner = (Window)this; but the result throws an exception that a Grid cannot be converted into a Window

采用

window.Owner = Window.GetWindow(this);
 for (int  index=0;index< App.Current.Windows.Count;index++ )
        {
            if (App.Current.Windows[index].Title == "MyWindow")
                control.Owner = App.Current.Windows[index];
        } 

here Title is the Title of Window which you want to set as owner.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM