如何从其他页面更改页面标签?

时间:2022-01-07 21:11:49

UPDATE: I have MainWindow, UC1 and UC2. Mainwindow contains frame1 and UCbutton than will show UC1 to the frame.

更新:我有MainWindow,UC1和UC2。 Mainwindow包含frame1和UCbutton,而不是将UC1显示到帧。

MainWindow:

<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

then upon openning UC1 there consists of textbox and button

然后在打开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

what i need is for the text in the textbox will be displayed in Label at UC2

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

UC2:

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

I already got help in navigating in to two page but i'm having a hard time learning Trycast or Directcast. :( Hope you could help me guys. Thanks...

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

2 个解决方案

#1


0  

Try this..

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

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

#2


0  

Use events.

In your child window (Page1) code create an event like the following.

在子窗口(Page1)代码中,创建如下事件。

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

On your button click in page1 use the following code.

在按钮上单击第1页,使用以下代码。

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

In your main page when you create Page1 add an event handler on the textchanged event

在创建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

#1


0  

Try this..

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

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

#2


0  

Use events.

In your child window (Page1) code create an event like the following.

在子窗口(Page1)代码中,创建如下事件。

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

On your button click in page1 use the following code.

在按钮上单击第1页,使用以下代码。

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

In your main page when you create Page1 add an event handler on the textchanged event

在创建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