如何在WPF应用程序中为页面创建模式对话框? [英] How do I make modal dialog for a Page in my WPF-application?

查看:93
本文介绍了如何在WPF应用程序中为页面创建模式对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF窗口,其中有一个其他控件托管一个Frame.在那个框架中,我显示了不同的页面.有没有办法使对话框模态化到仅页面?当我显示对话框时,应该不能单击页面上的任何控件,但是应该可以单击不在页面上的同一窗口上的控件.

I have a WPF Window which has a among other controls hosts a Frame. In that frame I display different pages. Is there way to make a dialog modal to only a page? When I'm showing the dialog it should not be possible to click on any control on the page but it should be possible to click on a control on the same window that is not on the page.

推荐答案

如果我在解释您的消息时是正确的,那么您想要的功能类似于 比莉·霍利斯(Billy Hollis)在其StaffLynx应用程序中进行演示.

If I am correct in interpreting your message, you want something that works similar to what Billy Hollis demonstrates in his StaffLynx application.

我最近建立了一个类似的控件,结果证明这种想法在WPF中实现起来相对简单.我创建了一个名为DialogPresenter的自定义控件.在自定义控件的控件模板中,我添加了类似于以下内容的标记:

I recently built a similar control and it turns out that this sort of idea is relatively simple to implement in WPF. I created a custom control called DialogPresenter. In the control template for the custom control, I added markup similar to the following:

<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属性中.

I also added a Show(Control view) method, which finds the the 'PART_DialogView', and adds the passed in view to the Content property.

然后,我可以按以下方式使用DialogPresenter:

This then allows me to use the DialogPresenter as follows:

<controls:DialogPresenter x:Name="DialogPresenter">
  <!-- Normal parent view content here -->
  <TextBlock>Hello World</TextBlock>
  <Button>Click Me!</Button>
</controls:DialogPresenter>

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

To the buttons event handler (or bound command), I simply call the Show() method of the DialogPresenter.

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

You can also easily add ScaleTransform markup to the DialogPresenter template to get scaling effects shown in the video. This solution has neat and tidy custom control code, and a very simple interface for your UI programming team.

希望这会有所帮助!

这篇关于如何在WPF应用程序中为页面创建模式对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆