简体   繁体   English

如何在WPF中从MainWindow访问ChildWindow TextBox

[英]How to access ChildWindow TextBox from MainWindow in WPF

I want to access ChildWindow TextBox from MainWindow. 我想从MainWindow访问ChildWindow TextBox。

MainWindow xaml codes are here; 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 codes are here; 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 codes are here; 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>

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

I need vb.net solution. 我需要vb.net解决方案。

The myChildWindow.ShowDialog() call blocks until that window returns. myChildWindow.ShowDialog()调用将阻塞,直到该窗口返回为止。 After that line of code you can access the child window's TextBox1 value with the following code because myChildWindow contains the Friend member TextBox1 which can be accessed in the MainWindow class: 在那行代码之后,您可以使用以下代码访问子窗口的TextBox1值,因为myChildWindow包含可在MainWindow类中访问的Friend成员TextBox1:

dim text as String = myChildWindow.TextBox1.text

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM