ChildWindow closed command

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

Hi,

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:

 

<basics:ChildWindow
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
xmlns:CArtData="clr-namespace:ComponentArt.Silverlight.UI.Data;assembly=ComponentArt.Silverlight.UI.Data"
xmlns:VM="clr-namespace:BusinessCube.VM"
xmlns:commands="clr-namespace:BusinessCube.VM.Commands"
x:Class="BusinessCube.Popup.PopupPDVNew"
Title=""

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

<basics:ChildWindow.Resources>
<VM:PopupVM x:Key="popupVM"></VM:PopupVM>
</basics:ChildWindow.Resources>

...
 

Now, in the view Model, I have:

 

public class PopupVM: ViewModelBase
{
public DelegateCommand<object> ClosedPopup { get; set; }
public PopupVM()
{
ClosedPopup = new DelegateCommand<object>
(
o =>
{
OnClosedPopup((ChildWindow)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 2:30 PM

Hi

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
http://blogs.southworks.net/dschenkelman