题
我有一个 WPF 窗口,其中有一个托管框架的控件。在该框架中我显示不同的页面。有没有办法使对话框仅在页面上模式化?当我显示对话框时,不应单击页面上的任何控件,但应该可以单击不在页面上的同一窗口上的控件。
解决方案
如果我在解释您的信息时是正确的,那么您需要的内容类似于什么 Billy Hollis在他的StaffLynx应用程序中演示。
我最近构建了一个类似的控件,结果发现这种想法在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>
我还添加了一个 Show(控制视图)
方法,该方法找到'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()方法。
您还可以轻松地将ScaleTransform标记添加到DialogPresenter模板,以获得视频中显示的缩放效果。该解决方案具有干净整洁的自定义控制代码,并为您的UI编程团队提供了一个非常简单的界面。
希望这有帮助!
其他提示
我有一个项目 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>
特征:
- 显示任意内容。
- 显示模态内容时不禁用主要内容。
- 显示模态内容时禁用鼠标和键盘访问主要内容。
- 仅适用于其所涵盖的内容,而不适用于整个应用程序。
- 可以通过绑定到 MVVM 友好的方式使用
IsModal
财产。
为什么不使用嵌套的消息泵来创建模态控件
您不是在寻找模态对话框。您需要一个能够禁用“页面”功能的功能。控件,显示对话框,并在对话框关闭时重新启用它。
我不太清楚你是否理解模态对话的意思是什么?
不隶属于 StackOverflow