Hosting a modal dialog in a ContentControl

我与影子孤独终老i 提交于 2019-12-13 04:10:16

问题


Instead of hosting my modal dialog at the center of my MainWindow, I would like to host it within one of the ContentControls within my MainWindow. Hope someone can advise me how to go about this. Thanks.


回答1:


I have an example on github of a custom FrameworkElement that allows you to display modal content over the primary content.

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 IsModal property.



回答2:


I have a custom UserControl built to accomplish this behavior. The article with the code is found here, and it can be used like this:

<local:PopupPanel 
    Content="{Binding PopupContent}"
    local:PopupPanel.PopupParent="{Binding ElementName=PopupParentPanel}"
    local:PopupPanel.IsPopupVisible="{Binding IsPopupVisible}" />


来源:https://stackoverflow.com/questions/9170094/hosting-a-modal-dialog-in-a-contentcontrol

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!