Skip to content

Instantly share code, notes, and snippets.

@nprokopic
Last active April 23, 2016 00:35
Show Gist options
  • Save nprokopic/e847506b629ecf07bc2c66659f02aec1 to your computer and use it in GitHub Desktop.
Save nprokopic/e847506b629ecf07bc2c66659f02aec1 to your computer and use it in GitHub Desktop.
Getting tapped view's ancestor by type
namespace MyApp
{
public class App : Application
{
// ...
public void OnSelectorTapped(object sender, EventArgs eventArgs)
{
var tappedElement = sender as Element;
if (tappedElement == null)
{
return;
}
NextPrevDateSelector tappedSelector = null;
Element parent = tappedElement.Parent;
while (parent != null)
{
if (parent is NextPrevDateSelector)
{
tappedSelector = parent as NextPrevDateSelector;
break;
}
parent = parent.Parent;
}
if (tappedSelector != null)
{
// Hopefully, if ControlTemplate + ContentView have wired up their respective
// layouts and views correctly, it will hit this line!
tappedSelector.NextDaySelected.Invoke();
// or
tappedSelector.PreviousDaySelected.Invoke();
// or if you need a command
object commandParameter = "command parameter if you need one, otherwise null";
if (tappedSelector.SomeCommand.CanExecute(commandParameter))
{
tappedSelector.SomeCommand.Execute(commandParameter);
}
// Check tappedElement in order to determine if you need next or previous day.
// You should find a way somehow to differentiate between next and previous views.
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment