Last active
April 23, 2016 00:35
-
-
Save nprokopic/e847506b629ecf07bc2c66659f02aec1 to your computer and use it in GitHub Desktop.
Getting tapped view's ancestor by type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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