我有一个WPF窗口,其中包含一个框架和其他控件。在该框架中,我显示不同的页面。有没有一种方法可以使对话框对某个页面具有模态性?当我显示对话框时,应该无法单击页面上的任何控件,但应该可以单击不在页面上的同一窗口上的控件。
我有一个WPF窗口,其中包含一个框架和其他控件。在该框架中,我显示不同的页面。有没有一种方法可以使对话框对某个页面具有模态性?当我显示对话框时,应该无法单击页面上的任何控件,但应该可以单击不在页面上的同一窗口上的控件。
<ControlTemplate TargetType="{x:Type local=DialogPresenter}">
<Grid>
<ContentControl>
<ContentPresenter />
</ContentControl>
<!-- The Rectangle is what simulates the modality -->
<Rectangle x:Name="Overlay" Visibility="Collapsed" Opacity="0.4" Fill="LightGrey" />
<Grid x:Name="Dialog" Visibility="Collapsed">
<!-- The template for the dialog goes here (borders and such...) -->
<ContentPresenter x:Name="PART_DialogView" />
</Grid>
</Grid>
<ControlTemplate.Triggers>
<!-- Triggers to change the visibility of the PART_DialogView and Overlay -->
</ControlTemplate.Triggers>
</ControlTemplate>
Show(Control view)
方法,该方法查找'PART_DialogView',并将传入的视图添加到Content
属性中。这样我就可以按照以下方式使用DialogPresenter
:<controls:DialogPresenter x:Name="DialogPresenter">
<!-- Normal parent view content here -->
<TextBlock>Hello World</TextBlock>
<Button>Click Me!</Button>
</controls:DialogPresenter>
DialogPresenter
的Show()方法即可。我在 Github 上有一个项目,它是一个自定义的 FrameworkElement
,可以让你在主要内容上方显示模态内容。
这个控件可以像这样使用:
<c:ModalContentPresenter IsModal="{Binding DialogIsVisible}">
<TabControl Margin="5">
<Button Margin="55"
Padding="10"
Command="{Binding ShowModalContentCommand}">
This is the primary Content
</Button>
</TabItem>
</TabControl>
<c:ModalContentPresenter.ModalContent>
<Button Margin="75"
Padding="50"
Command="{Binding HideModalContentCommand}">
This is the modal content
</Button>
</c:ModalContentPresenter.ModalContent>
</c:ModalContentPresenter>
特点:
IsModal
属性以MVVM友好的方式使用。你不需要一个模态对话框。你需要一个能够禁用“页面”控件、显示对话框并在对话框关闭时重新启用它的函数。
我不确定你是否理解模态对话框的作用?