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

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

问题描述

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

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天全站免登陆