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