WPF导航通过按钮

WPF导航通过按钮

问题描述:

问:有没有办法让一个按钮的行为像一个用户控件内的超链接WPF导航通过按钮


我一直在四处寻找了几天,并没有发现任何人谁处理了这一点。如何使用按钮在WPF应用程序中导航?具体来说,您如何在用户控件内部的按钮导航它的主机框?请记住,用户控件无法直接访问主机框架。如此简单:

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

不起作用。我正在使用用户控件。如果你只使用页面,这是你正在寻找的答案,如果你正在使用UserControls,请看下面的答案。

我觉得自己像一个混日子球回答我的问题,但我理解了它的持续多久!

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 

这个功能我发现这里(http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f8b02888-7e1f-42f4-83e5-448f2af3d207)将允许用户控制的内部使用的NavigationService的。

〜N

使用NavigationService..::.Navigate Method

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