WPF navigation via buttons

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


I have been looking around for several days and have not found anyone to address this. How do you use the button to navigate in a WPF application? In particular, how do you make a button inside a user control, move it in the main frame? Remember that user controls do not have direct access to the host file. so simple:

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

will not work. I use custom controls. If you use only pages, this is the answer you are looking for, if you use UserControls, see my answer below.

+3
source share
2 answers

I feel like a ball to answer my own question, but I realized it in the end!

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 that I found here ( http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f8b02888-7e1f-42f4-83e5-448f2af3d207 ) will allow you to use the NavigationService inside the user control.

~ N

+3
source

Use NavigationService .. ::. Go to the method :

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

Source: https://habr.com/ru/post/1719649/


All Articles