更新:我有 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。:(希望你们能帮助我。谢谢...