I am playing a Little with Windows 8 and building Windows Store Apps. Currently I am trying to create a popup. I want to tap on a button on a (primitive) user control which is hosted as the Child
of a Windows::UI::Xaml::Controls::Primitives::Popup
.
When I tapped on the button I want to do something and eventually close the popup. As I read here
Routed events outside the visual tree
... If you want to handle routed events from a Popup or ToolTip, place the handlers on specific UI elements that are within the Popup or ToolTip and not the Popup or ToolTip elements themselves. Don't rely on routing inside any compositing that is performed for Popup or ToolTip content. This is because event routing for routed events works only along the main visual tree. ...
I then created an ugly solution as to pass the popup (as parent) to my custom control.
void Rotate::MainPage::Image1_RightTapped_1(Platform::Object^ sender, Windows::UI::Xaml::Input::RightTappedRoutedEventArgs^ e)
{
Popup^ popup = ref new Popup();
ImagePopup^ popupchild = ref new ImagePopup(popup); //pass parent's reference to child
popupchild->Tapped += ref new TappedEventHandler(PopupButtonClick);
popup->SetValue(Canvas::LeftProperty, 100);
popup->SetValue(Canvas::TopProperty, 100);
popup->Child = popupchild;
popup->IsOpen = true;
}
void PopupButtonClick(Platform::Object^ sender, Windows::UI::Xaml::Input::TappedRoutedEventArgs^ e){
ImagePopup^ imgPop = static_cast<ImagePopup^>(sender);
if(imgPop != nullptr){
imgPop->CloseParent();
e->Handled = true;
}
e->Handled = false;
}
Is there any other way around? Thank you.