【发布时间】:2016-06-10 04:44:56
【问题描述】:
我无法将弹出窗口的所有者设置为应用程序主窗口。
这就是我所做的。
-
使用自定义 PopupWindow 创建了一个主窗口。
<Window x:Class="MainWindow" ... <Window.Resources> <Style x:Key="MessageWindowStyle" TargetType="{x:Type Window}"> <Setter Property="Background" Value="Transparent" /> <Setter Property="WindowStyle" Value="None" /> <Setter Property="ResizeMode" Value="NoResize" /> <Setter Property="BorderThickness" Value="1" /> <Setter Property="SnapsToDevicePixels" Value="True" /> <Setter Property="ShowInTaskbar" Value="False"/> <Setter Property="AllowsTransparency" Value="True"/> </Style> </Window.Resources> <i:Interaction.Triggers> <interactionRequest:InteractionRequestTrigger SourceObject="{Binding MessageRequest, Mode=OneWay}"> <interactionRequest:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True" WindowStyle="{StaticResource MessageWindowStyle}" > <interactionRequest:PopupWindowAction.WindowContent> <controls:PageCustomPopup /> </interactionRequest:PopupWindowAction.WindowContent> </interactionRequest:PopupWindowAction> </interactionRequest:InteractionRequestTrigger> </i:Interaction.Triggers> ... </Window> PageCustomPopup.xaml
这是弹出窗口的基本 xaml。
<UserControl x:Class="PageCustomPopup"
...
</UserControl>
- PageCustomPopup.xaml.cs
在此页面代码隐藏中,我尝试在加载弹出窗口时设置父级。但它会引发异常。
public partial class PageCustomPopup : UserControl, InteractionRequestAware, INotifyPropertyChanged
{
public PageCustomPopup()
{
Loaded += (sender, args) =>
{
try
{
var parentWindow = this.Parent as Window;
if (parentWindow != null)
{
// This line fails with "Cannot set the Owner after the window is displayed."
//parentWindow.Owner = Application.Current.MainWindow;
// First, default to the main window's dimensions
parentWindow.Width = Application.Current.MainWindow.Width;
parentWindow.Height = Application.Current.MainWindow.Height;
parentWindow.Left = Application.Current.MainWindow.Left;
parentWindow.Top = Application.Current.MainWindow.Top;
}
}
}
}
}
-
MainWindow 代码在后面。
public InteractionRequest<Notification> MessageRequest { get; private set; } public MainWindow() { ... MessageRequest = new InteractionRequest<Notification>(); ... } void Button_OnClick(object sender, EventArgs e) { MessageRequest.Raise(new Notification(){ Title = "Title Text", Content = "Message to Display"}); }
当自定义弹出窗口显示时,它会正确显示在主窗口的中心。
但问题是,当所有窗口最小化时,如果单击任务栏上的应用程序图标,则会显示应用程序主窗口,并且不可用。无法选择任何内容。
弹出窗口是隐藏的,不能放在前面。
将弹出窗口置于前面的唯一方法是使用“Alt+Tab”键。
我的问题是,如何将自定义弹出窗口所有者设置为 Application.Current.MainWindow? 这样,通过选择任务栏图标或 Alt+Tab 即可同时显示主窗口和弹出窗口。
【问题讨论】:
标签: wpf modal-dialog prism popupwindow