如何在WPF应用程序中为页面创建模式对话框?
我有一个WPF窗口,其中有一个控件和一个框架。在这个框架中,我显示了不同的页面。有没有办法使对话框只对一个页面进行模态化?当我显示对话框时,不应该可以单击页面上的任何控件,但应该可以单击不在页面上的同一窗口上的控件。您不是在这里寻找模式对话框。您需要一个功能来禁用“页面”控件,显示对话框,并在对话框关闭时重新启用它如何在WPF应用程序中为页面创建模式对话框?,wpf,modal-dialog,Wpf,Modal Dialog,我有一个WPF窗口,其中有一个控件和一个框架。在这个框架中,我显示了不同的页面。有没有办法使对话框只对一个页面进行模态化?当我显示对话框时,不应该可以单击页面上的任何控件,但应该可以单击不在页面上的同一窗口上的控件。您不是在这里寻找模式对话框。您需要一个功能来禁用“页面”控件,显示对话框,并在对话框关闭时重新启用它 不过,我不太确定您是否理解模态对话框的作用?如果我对您的信息的解释正确,您希望使用与以下内容类似的功能: 我最近构建了一个类似的控件,结果证明这种想法在WPF中实现起来相对简单。我
不过,我不太确定您是否理解模态对话框的作用?如果我对您的信息的解释正确,您希望使用与以下内容类似的功能: 我最近构建了一个类似的控件,结果证明这种想法在WPF中实现起来相对简单。我创建了一个名为DialogPresenter的自定义控件。在自定义控件的控件模板中,我添加了类似以下内容的标记:
<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>
对于按钮事件处理程序(或绑定命令),我只需调用DialogPresenter
的Show()方法
您还可以轻松地将ScaleTransform标记添加到DialogPresenter模板,以获得视频中显示的缩放效果。此解决方案具有整洁的自定义控制代码,并为UI编程团队提供了一个非常简单的界面
希望这有帮助 为什么不使用嵌套的消息泵来创建模式控件 我有一个项目,其中有一个自定义的
框架元素
,允许您在主要内容上显示模式内容
控件可以这样使用:
<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>
这是主要内容
这是情态内容
特点:
- 显示任意内容
- 显示模式内容时不禁用主要内容
- 显示模式内容时,禁用鼠标和键盘对主要内容的访问
- 是它所覆盖的内容的唯一模态,而不是整个应用程序
- 可以通过绑定到
属性以MVVM友好的方式使用李>IsModal
<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>