通过按钮WPF导航

时间:2009-10-08 18:41:04

标签: wpf button navigation

问题:有没有办法让按钮的行为类似于用户控件中的超链接


我一直在寻找几天,并没有找到任何解决过这个问题的人。如何使用按钮在WPF应用程序中导航?具体来说,如何在用户控件内部创建一个按钮来导航它的主机框架?请记住,用户控件无法直接访问主机框架。简单地说:

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

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

2 个答案:

答案 0 :(得分:3)

我觉得这是一个回答我自己的问题的球,但我终于明白了!

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的

答案 1 :(得分:1)

使用NavigationService..::.Navigate Method

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