开发者

How to make ContentControl listen to non-routed event inside its ContentTemplate

开发者 https://www.devze.com 2022-12-13 05:52 出处:网络
I have some reusable ContentControl, which acts as a pseudo开发者_StackOverflow中文版-modal popup inside another view.

I have some reusable ContentControl, which acts as a pseudo开发者_StackOverflow中文版-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?


How about making the ICloseMe include a Closed property that you could setup a trigger against in the XAML?

0

精彩评论

暂无评论...
验证码 换一张
取 消