通过按钮进行 WPF 导航

发布于 2024-08-07 01:50:50 字数 302 浏览 1 评论 0原文

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


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

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

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

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


I've been searching around for a few days now and haven't found anyone who has addressed this. How do you use a button to navigate in a WPF application? Specifically how do you make a button inside of a user control navigate it's host frame? Bare in mind that User controls do not have direct access to the host frame. so simply:

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

won't work. I'm using user controls. If you are using only pages, this is the answer you are looking for, if you are using UserControls, look at my answer below.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

留一抹残留的笑 2024-08-14 01:50:50

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

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

I feel like a goof ball for answering my own question, but i figured it out at long last!

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

~N

蓝咒 2024-08-14 01:50:50

使用 NavigationService..::.Navigate 方法

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

Use the NavigationService..::.Navigate Method:

void goButton_Click(object sender, RoutedEventArgs e)
{
   this.NavigationService.Navigate(new Uri(this.addressTextBox.Text));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文