I have some reusable ContentControl, which acts as a pseudo-modal popup inside another view. It is always there and only its visibility makes it appear or disappear. When instantiated, within the ContentControl there will be a custom ContentTemplate, bound to some ViewModel and representing the content of the "modal popup".
<Dialogs:ModalDialog DialogHost="{Binding ElementName=layoutRoot, Mode=OneTime}"
Content="{Binding ViewModel.CurrentEditItem}"
IsShown="{Binding ViewModel.IsInEdit}">
<Dialogs:ModalDialog.ContentTemplate>
<DataTemplate>
<ItemEditor:ItemEditorView />
</DataTemplate>
</Dialogs:ModalDialog.ContentTemplate>
</Dialogs:ModalDialog>
Now I want to reach the following: the root of the ContentTemplate (here: ItemEditorView) should implement the following interface.
public interface ICloseMe
{
event EventHandler<EventArgs> CloseMe;
}
Whenever the CloseMe-Event is fired, the surrounding ModalDialog should be "closed" by setting its VisibilityProperty to Hidden.
The view within the popup (here ItemEditorView) should not care, whether it is shown in a ModalDialog or another context, i.e. it should not even know that such class exists. This excludes a walk through the Logical or Visual tree. It shall only fire the CloseMe-Event, when Cancel/Save-Buttons are pressed.
Further, the mechanism should not be implemented/configured in the view instantiating the ModalDialog, the view should be as dumb as possible.
Instead, the "outer" ModalDialog should do the active part and listen to the CloseMe-event. How can I implement this in a rather clean, MVVM-compliant way and without introducing unnecessary dependencies? Is there any event, occuring after a ContentTemplate is initialized, s.t. the ModalDialog could then evaluate, if the root of it extends ICloseMe?