如何在WPF中从MainWindow访问ChildWindow TextBox

时间:2018-11-24 23:11:51

标签: c# wpf vb.net

我想从MainWindow访问ChildWindow TextBox。

MainWindow xaml代码在这里;

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
    <Button x:Name="Button1" Height="20" Width="100" Content="Click Me"/>
</Grid>
</Window>

MainWindow vb.net代码在这里;

Class MainWindow 
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
    TextBox1.Text = "Hello"
    Dim myChildWindow As New ChildWindow()
    myChildWindow.Owner = Me
    myChildWindow.ShowDialog()
End Sub
End Class

ChildWindow xaml代码在这里;

<Window x:Class="ChildWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ChildWindow" Height="300" Width="300">
<Grid>
    <TextBox x:Name="TextBox1" Height="20" Width="100" Text=""/>
</Grid>
</Window>

这里有一个C#解决方案:https://stackoverflow.com/a/2219218/10690106

我需要vb.net解决方案。

1 个答案:

答案 0 :(得分:0)

myChildWindow.ShowDialog()调用将阻塞,直到该窗口返回为止。在那行代码之后,您可以使用以下代码访问子窗口的TextBox1值,因为myChildWindow包含可在MainWindow类中访问的Friend成员TextBox1:

PRIMARY KEY
相关问题