PopupBehavior + childwindow support
Jun
2
Written by:
6/2/2011 9:15 PM
I am digging up my old code for this, and rushed to get this post.
Last post gave this one a preview, and some things just wouldn’t work and some did.
Demo page
Get the Code
What makes this one better/different (Compares to Mod Popup)
1) This one allows you to make your very own popup (ChildWindow) instead of just the inside.
2) doesn’t use HisowaModPopupResult data holder class. Instead you can type in the property name in PopUpVM as parameter of ICommand
3) Pop up is now created by the behavior every time. not reused which caused the new Popup to have old values.
This will supplement some info from the last post.

- View models Folder – has view model for Child and Main VM
- Childwindow1.xaml – Your custom childwindow
- PopUpProtoV2 – our new behavior
- DataObjectContainer – holds data, number and text
PopUpProtoV2 code
///DELETED SOME IRRELEVENT CODE FOR BLOG POST
///DELETED SOME IRRELEVENT CODE FOR BLOG POST
///DELETED SOME IRRELEVENT CODE FOR BLOG POST
namespace PopupBehaviorProto
{
/// <summary>
/// Prototype ... hisowa
/// </summary>
[System.ComponentModel.Description("Launches a Popup with custom UI on Event Trigger")]
public class PopUpProtoV2 : TargetedTriggerAction<object>, INotifyPropertyChanged
{
#region propertyname
public static readonly DependencyProperty PropertyNameProperty = DependencyProperty.Register("PropertyName", typeof(string), typeof(PopUpProtoV2), null);
public string PropertyName
{
get
{
return (string)base.GetValue(PropertyNameProperty);
}
set
{
base.SetValue(PropertyNameProperty, value);
}
}
public static readonly DependencyProperty CustomChildWindowProperty = DependencyProperty.Register("CustomChildWindow", typeof(ChildWindow), typeof(PopUpProtoV2), null);
public ChildWindow CustomChildWindow
{
get
{
return (ChildWindow)base.GetValue(CustomChildWindowProperty);
}
set
{
base.SetValue(CustomChildWindowProperty, value);
}
}
#endregion
ChildWindow ChildPopUp;
#region Invoke
//Shows popup on event trigger you set in designer
protected override void Invoke(object parameter)
{
ChildPopUp = (ChildWindow)Activator.CreateInstance(CustomChildWindow.GetType());
ChildPopUp.Show();
ChildPopUp.Closing += new EventHandler<CancelEventArgs>(ChildPopUp_Closing);
}
#endregion
#region Events
void ChildPopUp_Closing(object sender, CancelEventArgs e)
{
if (ReturnICommand != null && ChildPopUp.DialogResult.HasValue && ChildPopUp.DialogResult.Value)
{
//need to get the data context's prop. not window.
Panel pnl = ChildPopUp.GetType().GetProperty("Content").GetValue(ChildPopUp, null) as Panel;
if (pnl != null)
{
object dc = ((FrameworkElement)pnl.Children[0]).DataContext;
//return the data property
ReturnICommand.Execute(dc.GetType().GetProperty(PropertyName).GetValue(dc, null));
}
}
RemoveHandlers();
}
private void RemoveHandlers()
{
ChildPopUp.Closing -= new EventHandler<CancelEventArgs>(ChildPopUp_Closing);
}
#endregion
}
}
The Key points here is how the childwindow is getting created. Its now using an activator, its not getting reused so the state of the last pop up will not show up again. In the closing event I am getting the panel (grid, stackpanel,etc) and extracting the datacontext from it. Then I pass the property value in the return ICommand. PropertyName is from the dependency property and set by typing the name of the property to extract in the behavior property.
Properties of the Behavior

- CustomChildWindowProperty – the childwindow you want to show
- PropertyName – Name of the property you want to pass back from the ChildViewModel’s property
- ReturnICommand – The ICommand on the MainViewModel that you want to run, takes the PropertyName’s value as parameter
This demo ReturnICommand adds “cmd” to the string and + 2 to the int.
The childVM has the ChildDataObject property which is a DataObjectContainer.
//that pretty much sums it up.
//Go write your own code behind or edit my stuff all you like to meet your goals.
//As for passing parameters to the child View model I haven’t found a solution to it yet.