1

更新:我有 MainWindow、UC1 和 UC2。主窗口包含 frame1 和 UCbutton,而不是将 UC1 显示到框架。

主窗口:

<Frame Height="200" HorizontalAlignment="Left" Margin="10,10,0,0" Name="Frame1" VerticalAlignment="Top" Width="400" />
<Button Content="Show Usercontrol" Height="23" HorizontalAlignment="Left" Margin="12,216,0,0" Name="SUbutton" VerticalAlignment="Top" Width="120" />

VB:

Private Sub SUbutton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles SUbutton.Click
    Dim uc1 As New UC1
    Frame1.Navigate(uc1)
End Sub

然后在打开 UC1 时,由文本框和按钮组成

UC1:

<TextBlock Height="26" HorizontalAlignment="Left" Margin="12,45,0,0" Text="Page1" VerticalAlignment="Top" Width="40" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="12,77,0,0" Name="TextBox1" VerticalAlignment="Top" Width="120" />
<Button Content="Show Usercontrol2" Height="23" HorizontalAlignment="Left" Margin="12,106,0,0" Name="SU2button" VerticalAlignment="Top" Width="120" />

VB:

Private Sub SU2button_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles SU2button.Click
    Dim mainWindow = GetParentWindow(Me)
    If mainWindow IsNot Nothing Then
        mainWindow.Frame1.Navigate(New UC2())
    End If
End Sub
Private Shared Function GetParentWindow(ByVal obj As DependencyObject) As MainWindow
    While obj IsNot Nothing
        Dim mainWindow = TryCast(obj, MainWindow)
        If mainWindow IsNot Nothing Then
            Return mainWindow
        End If
        obj = VisualTreeHelper.GetParent(obj)
    End While
    Return Nothing
End Function

我需要的是文本框中的文本将显示在 UC2 的标签中

UC2:

<TextBlock Height="31" HorizontalAlignment="Left" Margin="37,92,0,0" Name="hello" VerticalAlignment="Top" Width="220" />

我已经在导航到两页时获得了帮助,但我很难学习 Trycast 或 Directcast。:(希望你们能帮助我。谢谢...

4

2 回答 2

0

尝试这个..

private void Button2_Click(object sender, RoutedEventArgs e) { var wnd = Window.GetWindow(this); ((StackOverflow.MainWindow)(wnd)).Label1.Content = "sadasd"; }

于 2013-03-04T07:28:55.680 回答
0

使用事件。

在您的子窗口 (Page1) 代码中创建一个如下所示的事件。

Public Event Textchanged(sender as object, text as String)

在您的按钮上单击 page1 使用以下代码。

Private Sub Button2_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button2.Click
RaiseEvent Textchanged(Me, Textbox2.Text)
End Sub

在您创建 Page1 时的主页面中,在 textchanged 事件上添加一个事件处理程序

Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
    Dim page1 As New Page1
    Addhandler page1.Textchanged, AddressOf Changetext
    Frame1.Navigate(page1)
End Sub

Private Sub ChangeText(sender as object, txt as String)
Label1.Content = txt
End Sub
于 2013-03-04T09:50:44.883 回答