更新:我有MainWindow,UC1和UC2。 Mainwindow包含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。 :(希望你能帮助我们。谢谢......
答案 0 :(得分:0)
试试这个..
private void Button2_Click(对象发送者,RoutedEventArgs e) { var wnd = Window.GetWindow(this); ((StackOverflow.MainWindow)(wnd))。Label1.Content =“sadasd”; }
答案 1 :(得分:0)
使用活动。
在您的子窗口(第1页)代码中,创建如下事件。
Public Event Textchanged(sender as object, text as String)
点击第1页的按钮,使用以下代码。
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