代码之家  ›  专栏  ›  技术社区  ›  Robert Höglund

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

  •  25
  • Robert Höglund  · 技术社区  · 16 年前

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

    4 回复  |  直到 8 年前
        1
  •  25
  •   Brad Leach    16 年前

    如果我对你的信息的解释是正确的,你需要一些类似于 Billy Hollis demonstrates in his StaffLynx application .

    我最近构建了一个类似的控件,结果发现这种想法在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(Control view) 方法,该方法查找“part\u dialogview”,并将传入的视图添加到 Content 财产。

    这样我就可以使用 DialogPresenter 如下:

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

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

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

    希望这有帮助!

        2
  •  4
  •   Benjamin Gale    8 年前

    我有一个项目 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>
    

    特征:

    • 显示任意内容。
    • 在显示模式内容时不禁用主要内容。
    • 在显示模式内容时禁用对主要内容的鼠标和键盘访问。
    • 只是对它所覆盖的内容的模式,而不是整个应用程序。
    • 可以通过绑定到 IsModal 财产。
        3
  •  2
  •   Pang Ajmal PraveeN    9 年前

    为什么不使用嵌套消息泵来创建模式控件呢?

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

        4
  •  1
  •   Mez    16 年前

    您不需要在此处查找模式对话框。您需要一个函数来禁用“页面”控件,显示对话框,并在对话框关闭时重新启用它。

    我不太确定你是否理解模态对话的含义?