在WPF中的UserControl中设置Window所有者

时间:2013-08-19 03:34:24

标签: c# wpf window parent owner

我有一个UserControl在任务栏中显示一个没有图标的窗口,问题是当主窗口失去焦点并再次获取它时,没有任务栏图标的窗口位于主窗口后面,很难显示它在前面,当类是一个窗口时,使用window.Owner = this;来解决此问题,但是当UserControl或其他类型(例如ListBoxItemViewbox)我可以'这样做。我尝试执行此操作control.Owner = (Window)this;,但结果会引发一个异常,即Grid无法转换为Window

2 个答案:

答案 0 :(得分:7)

使用

window.Owner = Window.GetWindow(this);

答案 1 :(得分:1)

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

此处标题是您要设置为所有者的窗口标题。