简体   繁体   中英

How to access ChildWindow TextBox from MainWindow in WPF

I want to access ChildWindow TextBox from MainWindow.

MainWindow xaml codes are here;

<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;

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;

<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

I need vb.net solution.

The myChildWindow.ShowDialog() call blocks until that window returns. 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:

dim text as String = myChildWindow.TextBox1.text

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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