ChildWindow closed command

Topics: Prism v2 - Silverlight 3
Sep 4, 2009 at 4:28 PM


I have a custom user control composed by a ChildWindow containing some controls. I manage this Child Window through a ViewModel and I use Prism library in order to implement commanding. With other controls, all works correctly, but with this ChildWindow I have a problem.

 I created the custom ChildWindowClosed command (through common files pair: ChildWindowClosed.cs and ChildWIndowClosedBehaviour.cs), then I attached this command to the ChildWindow in this way:



DataContext="{Binding Path=., Source={StaticResource popupVM}}"
commands:ChildWindowClosed.Command="{Binding ClosedPopup}"
commands:ChildWindowClosed.CommandParameter="{Binding RelativeSource={RelativeSource Self}}"

<VM:PopupVM x:Key="popupVM"></VM:PopupVM>


Now, in the view Model, I have:


public class PopupVM: ViewModelBase
public DelegateCommand<object> ClosedPopup { get; set; }
public PopupVM()
ClosedPopup = new DelegateCommand<object>
o =>
o => true

public void OnClosedPopup(ChildWindow childWindow)
//do something

 When I click on the OK button in the childWindow, I set  (for now, in the code behind..I have to move also this method in the view model)

 this.DialogResult = true;

then the closed event is called (I know this because I tried to attach, for test, an event handler on the childwindow.closed, in the code behind) but the ViewModel is not able to manage it, I mean, the OnClosedPopup is not called.

Some helps?

Sep 7, 2009 at 3:30 PM


I have created a small sample application that executes a command in the ChildWindow's ViewModel when it is closed. The attached behavior for the command was created using this snippet. You can download the sample application from here.

You might consider using the Closing event instead of the Closed event. This will depend on your requirements, but remember that the Closing event is fired before the ChildWindow is closed, while the Closed event is raised after.

Please let me know if this helps.

Damian Schenkelman