【问题标题】:WPF navigation via buttons通过按钮进行 WPF 导航
【发布时间】:2010-12-05 02:41:08
【问题描述】:

问题:有没有办法让按钮在用户控件中表现得像一个超链接


我已经找了几天了,还没有找到解决这个问题的人。如何使用按钮在 WPF 应用程序中导航?具体来说,您如何使用户控件内的按钮导航其主机框架?请记住,用户控件不能直接访问主机框架。很简单:

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

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

【问题讨论】:

    标签: wpf button navigation


    【解决方案1】:

    我觉得自己回答自己的问题是个傻瓜,但我终于想通了!

    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

    【讨论】:

      【解决方案2】:

      使用NavigationService..::.Navigate Method

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

      【讨论】:

        猜你喜欢
        • 2014-04-05
        • 2013-08-12
        • 1970-01-01
        • 2022-06-24
        • 1970-01-01
        • 2016-03-09
        • 1970-01-01
        • 1970-01-01
        • 2020-05-18
        相关资源
        最近更新 更多