tags:

views:

886

answers:

2

Question: Is there a way to make a button behave like a hyperlink inside of a user control?


I've been searching around for a few days now and haven't found anyone who has addressed this. How do you use a button to navigate in a WPF application? Specifically how do you make a button inside of a user control navigate it's host frame? Bare in mind that User controls do not have direct access to the host frame. so simply:

this.NavigationService.Navigate(new Uri(this.addressTextBox.Text));

won't work. I'm using user controls. If you are using only pages, this is the answer you are looking for, if you are using UserControls, look at my answer below.

+1  A: 

Use the NavigationService..::.Navigate Method:

void goButton_Click(object sender, RoutedEventArgs e)
{
   this.NavigationService.Navigate(new Uri(this.addressTextBox.Text));
}
luvieere
+1  A: 

I feel like a goof ball for answering my own question, but i figured it out at long last!

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
    Dim pg As Page = CType(GetDependencyObjectFromVisualTree(Me, GetType(Page)), Page)
    Dim newPage As %desired uri here% = New %desired uri here% 
    pg.NavigationService.Navigate(newPage)
End Sub

Private Function GetDependencyObjectFromVisualTree(ByVal startObject As DependencyObject, ByVal type As Type) As DependencyObject
    'Walk the visual tree to get the parent(ItemsControl)
    'of this control
    Dim parent As DependencyObject = startObject

    While (Not (parent) Is Nothing)
        If type.IsInstanceOfType(parent) Then
            Exit While
        Else
            parent = VisualTreeHelper.GetParent(parent)
        End If

    End While
    Return parent
End Function

This function i found here (http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f8b02888-7e1f-42f4-83e5-448f2af3d207) will allow for the use of NavigationService inside of a user control.

~N

Narcolapser