使FloatingWindow成为模态
本文关键字:模态 FloatingWindow | 更新日期: 2023-09-27 18:17:52
我使用AvalonDock
控件将我的视图显示为选项卡。在某些情况下,我想要打开一个新窗口。目前,我正在处理LayoutInitializer:
public void AfterInsertDocument(LayoutRoot layout, LayoutDocument anchorableShown)
{
if (anchorableShown.Content != null && anchorableShown.Content is ViewModelBase)
{
var viewModel = ((ViewModelBase)anchorableShown.Content);
if (viewModel.Type == ViewModelBase.ViewType.Popup)
{
anchorableShown.FloatingWidth = viewModel.PopupWidth;
anchorableShown.FloatingHeight = viewModel.PopupHeight;
anchorableShown.FloatingTop = viewModel.PopupTop;
anchorableShown.FloatingLeft = viewModel.PopupLeft;
anchorableShown.Float();
}
}
}
很好。但我想让这个新的浮动窗口成为模态窗口。而且不能停靠。我不知道该怎么写
最好的方法是创建一个单独的窗口。
下面是我的XAML定义:<Window ... >
<Window.Resources>
<DataTemplate DataType="{x:Type local:FirstViewModel}">
<local:FirstView />
</DataTemplate>
<DataTemplate DataType="{x:Type local:SecondViewModel}">
<local:SecondView />
</DataTemplate>
</Window.Resources>
<DockPanel LastChildFill="True" Name="mainPanel" >
<ContentPresenter Content="{Binding}">
</ContentPresenter>
</DockPanel>
</Window>
Foreach ViewModel/View赋值我使用这个DataTemplate
块。
WindowManager
实现中,我创建了一个新窗口:
private System.Windows.Window CreateWindow(ViewModelBase viewModel)
{
var window = new PopupWindow();
window.DataContext = viewModel;
return window;
}
…并显示为模态窗口:
public void ShowWindow(ViewModelBase viewModel)
{
var window = CreateWindow(viewModel);
window.ShowDialog();
}