问:是否有一种方法可以使按钮在用户控件中表现得像超链接?
我已经找了几天了,还没找到任何解决这个问题的人。如何在WPF应用程序中使用按钮导航?具体而言,如何在用户控件内部设置一个按钮,导航它的主机帧?请记住,用户控件不能直接访问主机帧。所以简单地说:
this.NavigationService.Navigate(new Uri(this.addressTextBox.Text));不起作用。我在使用用户控件。如果您只使用页面,这是您正在寻找的答案,如果您使用的是UserControls,请看下面的答案。
发布于 2009-10-08 19:56:51
我觉得自己回答自己的问题就像个傻瓜,但我终于明白了!
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
发布于 2009-10-08 18:46:59
使用NavigationService..::.Navigate法
void goButton_Click(object sender, RoutedEventArgs e)
{
   this.NavigationService.Navigate(new Uri(this.addressTextBox.Text));
}https://stackoverflow.com/questions/1539698
复制相似问题