【问题标题】:Set Window owner inside of an UserControl in WPF在 WPF 中的 UserControl 内设置窗口所有者
【发布时间】:2013-08-20 19:00:27
【问题描述】:

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

【问题讨论】:

  • 也许control.Owner = Application.Current.MainWindow;
  • 或者你可以通过 VisualTreeHelper 找到可视父窗口。

标签: c# wpf window parent owner


【解决方案1】:

使用

window.Owner = Window.GetWindow(this);

【讨论】:

    【解决方案2】:
     for (int  index=0;index< App.Current.Windows.Count;index++ )
            {
                if (App.Current.Windows[index].Title == "MyWindow")
                    control.Owner = App.Current.Windows[index];
            } 
    

    这里的 Title 是你想设置为所有者的窗口的标题。

    【讨论】:

    • 感谢您的回答,这并不完全是我想要解决的问题,但我发现它在我正在创建的应用程序的某些部分很有用,谢谢,我会给 +1。
    猜你喜欢
    • 1970-01-01
    • 2014-10-12
    • 2021-08-25
    • 2011-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多