我不知道为什么它不能使用我在这里找到的关于这个问题的任何线程。我只想从另一个窗口访问Label。所以这是MainWindow
Public Class MainWindow
Private WithEvents _popUpBox As NewCompany
Private Sub Button_NewGame_Click(sender As Object, e As RoutedEventArgs) Handles Button_NewGame.Click
If _popUpBox Is Nothing Then
_popUpBox = New NewCompany
_popUpBox.ShowDialog()
Else
_popUpBox.Activate()
End If
End Sub
End Class
和NewCompany / popUpBox
Public Class NewCompany
Private Sub Button_Confirm_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button_Confirm.Click
MainWindow.Label_Company.Content = "Some Company"
End Sub
End Class
我知道它是MVVM,它与Windows Forms完全不同。所以也许有人可以用这个小例子来帮助我。
那么如何更改此Label的内容?
答案 0 :(得分:1)
在打开PopupBox之前,将Owner
设置为MainWindow
,然后您可以使用所有者属性从弹出窗口访问它。
主窗口
_popUpBox = New NewCompany
_popUpBox.Owner = Me
_popUpBox.ShowDialog()
弹出窗口
DirectCast(Owner, MainWindow).Label_Company.Content = "Some Company"