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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/173652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:41:18  来源:igfitidea点击:

How do I make modal dialog for a Page in my WPF-application?

wpfmodal-dialog

提问by Robert H?glund

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.

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

采纳答案by Brad Leach

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

如果我对您的消息的解释是正确的,那么您想要的东西类似于 Billy Hollis 在他的 StaffLynx 应用程序中展示的东西

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:

我最近构建了一个类似的控件,结果证明这种想法在 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>

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

我还添加了一个Show(Control view)方法,该方法查找“PART_DialogView”,并将传入的视图添加到Content属性中。

This then allows me to use the DialogPresenteras follows:

这允许我使用DialogPresenter如下:

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

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

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

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.

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

Hope this helps!

希望这可以帮助!

回答by Benjamin Gale

I have a project on githubwhich is a custom FrameworkElementthat allows you to display modal content over the primary content.

我在github上有一个项目,它是一个自定义项目FrameworkElement,允许您在主要内容上显示模态内容。

The control can be used like this:

控件可以这样使用:

<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>

Features:

特征:

  • Displays arbitrary content.
  • Does not disable the primary content whilst the modal content is being displayed.
  • Disables mouse and keyboard access to the primary content whilst the modal content is displayed.
  • Is only modal to the content it is covering, not the entire application.
  • can be used in an MVVM friendly way by binding to the IsModalproperty.
  • 显示任意内容。
  • 在显示模式内容时不禁用主要内容。
  • 在显示模式内容时禁用对主要内容的鼠标和键盘访问。
  • 仅适用于它所涵盖的内容,而不是整个应用程序。
  • 可以通过绑定到IsModal属性以 MVVM 友好的方式使用。

回答by Dean Chalk

Why not just use nested message pumps to create modal controls

为什么不直接使用嵌套消息泵来创建模态控件

http://deanchalk.com/wpf-modal-controls-via-dispatcherframe-nested-message-pumps/

http://deanchalk.com/wpf-modal-controls-via-dispatcherframe-nested-message-pumps/

回答by Mez

You are not looking for a modal dialog here. You need a function that will disable the "page" control, show a dialog, and re-enable it when the dialog closes.

您不是在这里寻找模态对话框。您需要一个功能来禁用“页面”控件,显示一个对话框,并在对话框关闭时重新启用它。

I'm not too sure whether you understand what a modal dialog is meant to do though?

我不太确定你是否理解模态对话框的意思?