如何在WPF应用程序中为页面创建模态对话框?

Rob*_*und 25 wpf modal-dialog

我有一个WPF窗口,其中有一个控件主机框架.在那个框架中,我显示不同的页面.有没有办法只对一个页面进行对话模式?当我显示对话框时,不应该单击页面上的任何控件,但应该可以单击页面上不在同一窗口上的控件.

Bra*_*ach 25

如果我在解释您的信息时是正确的,那么您需要的东西与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>
Run Code Online (Sandbox Code Playgroud)

我还添加了一个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>
Run Code Online (Sandbox Code Playgroud)

对于按钮事件处理程序(或绑定命令),我只需调用该方法的Show()方法DialogPresenter.

您还可以轻松地将ScaleTransform标记添加到DialogPresenter模板,以获得视频中显示的缩放效果.该解决方案具有干净整洁的自定义控制代码,并为您的UI编程团队提供了一个非常简单的界面.

希望这可以帮助!