Skip to content

Instantly share code, notes, and snippets.

@sonnemaf
Created October 13, 2015 08:56
Show Gist options
  • Save sonnemaf/0d1c6fbc3cfc4e2310c4 to your computer and use it in GitHub Desktop.
Save sonnemaf/0d1c6fbc3cfc4e2310c4 to your computer and use it in GitHub Desktop.
NavigateBackAction XAML Behavior for Blend
public class NavigateBackAction : DependencyObject, IAction {
/// <summary>
/// Navigate the Frame back
/// </summary>
/// <param name="sender"></param>
/// <param name="parameter"></param>
/// <returns>True if navigated</returns>
public object Execute(object sender, object parameter) {
var dependencyObject = sender as DependencyObject;
var navigate = dependencyObject as INavigate;
while (dependencyObject != null && navigate == null) {
navigate = (dependencyObject as INavigate);
if (navigate == null) {
dependencyObject = VisualTreeHelper.GetParent(dependencyObject);
}
}
if (navigate == null) {
return false;
}
var frame = navigate as Frame;
if (frame != null && frame.CanGoBack) {
frame.GoBack();
return true;
}
return false;
}
}
@marcelodeaguiar
Copy link

Why you search for an INavigate and not the Frame directly? You are casting it anyways

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment