public void BeforeOnApplyTemplate()
{
if(UsesClosingVisualState)
{
// Unhook the event handler for the popup closed visual state group.
// This code is used to enable visual state transitions before
// actually setting the underlying Popup.IsOpen property to false.
VisualStateGroup groupPopupClosed = VisualStates.TryGetVisualStateGroup(Parent, VisualStates.GroupPopup);
if(null != groupPopupClosed)
{
groupPopupClosed.CurrentStateChanged -= OnPopupClosedStateChanged;
UsesClosingVisualState = false;
}
}
if(Popup != null)
{
Popup.Closed -= Popup_Closed;
}
}