当前位置:网站首页>Realize the mutual value transfer between main window and sub window in WPF
Realize the mutual value transfer between main window and sub window in WPF
2022-07-28 13:26:00 【Willard Leozi】
In the use of WPF When , We often use the transfer of values between forms , The following example transfers values from the main window to the sub window , The method of transferring values from the sub window to the main window .
One 、 The main window transfers values to the sub window
The main method of transferring values from the main window to the sub window is to create a variable in the sub window to receive the values of the main window , Then realize the value transmission .
In the main window Xaml Code :
<Grid>
<TextBox x:Name="tb_message" HorizontalAlignment="Left" Height="35" Margin="232,80,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="360" FontSize="16"/>
<Button Content=" Pass values to subform " FontSize="20" HorizontalAlignment="Left" Margin="342,179,0,0" VerticalAlignment="Top" Width="124" Height="34" Click="Button_Click"/>
</Grid>View :

Main window background code :
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
child_window child_Window = new child_window();
child_Window.test_str = tb_message.Text;
child_Window.Show();
this.Close();
}
}Of a child window Xaml Code :
<Grid>
<TextBlock x:Name="tb_receive" HorizontalAlignment="Left" Margin="249,75,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="29" Width="316"/>
<Button Content=" Receive the value from the main window :" FontSize="15" HorizontalAlignment="Left" Margin="319,168,0,0" VerticalAlignment="Top" Width="189" Height="29" Click="Button_Click"/>
</Grid>View :

Sub window background code :
public partial class child_window : Window
{
public string test_str { get; set; }
public child_window()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
tb_receive.Text = test_str;
}
}Demonstration of value transfer effect from main window to sub window :


Two 、 The sub window transfers values to the main window
The transfer of values from the sub form to the main form is mainly executed through delegation .
Of the main form Xaml Code :
<Grid>
<Button Content=" Open sub window :" FontSize="15" HorizontalAlignment="Left" Margin="43,78,0,0" VerticalAlignment="Top" Width="287" Height="39" RenderTransformOrigin="0.5,0.5" Click="Button_Click"></Button>
<TextBlock x:Name="tb_recevie" HorizontalAlignment="Left" Margin="395,78,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="349"/>
</Grid>View :

Main window background code :
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public void Recevie(string value)
{
tb_recevie.Text = value;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
child_window child_Window = new child_window();
child_Window.sendMessage = Recevie;
child_Window.Show();
}
}Of a child window Xaml Code :
<Grid>
<TextBox x:Name="tb_send" HorizontalAlignment="Left" Height="33" Margin="230,101,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="329"/>
<Button Content=" Pass values to the main form " FontSize="15" HorizontalAlignment="Left" Margin="317,181,0,0" VerticalAlignment="Top" Width="164" Height="27" Click="Button_Click"/>
</Grid>View :

Sub window background code :
public partial class child_window : Window
{
public delegate void SendMessage(string value);
public SendMessage sendMessage;
public child_window()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
sendMessage(tb_send.Text);
}
}Demonstration of value transfer effect from sub window to main window :



边栏推荐
- Leetcode daily question (2196. create binary tree from descriptions)
- FFT wave simulation
- LeetCode·每日一题·1331.数组序号转换·离散化
- 8、 Kubernetes network and load balancing
- [FPGA]: ise and Modelsim joint simulation
- Aragon creates Dao polygon BSC test network
- Mysql中DQL基本练习
- Using auto.js to realize fifaol3 brush teaching assistant
- [FPGA] joint simulation of vivado and Modelsim
- Understanding of vite2
猜你喜欢

Aragon创建DAO polygon BSC测试网

夜神模拟器抓包微信小程序
![[报错]使用ssh登陆到另一台机器后,发现主机名还是自己|无法访问yarn8088](/img/81/641a5b3445534fc3b8c87ee6deaa64.png)
[报错]使用ssh登陆到另一台机器后,发现主机名还是自己|无法访问yarn8088

Shell basic concepts and variables

如何在 TiDB Cloud 上使用 Databricks 进行数据分析 | TiDB Cloud 使用指南

Leetcode-136. numbers that appear only once

火山石投资章苏阳:硬科技,下一个10年相对确定的答案

二舅能治好年轻人的精神内耗吗?

The essence of enterprise Digitalization

Shell基础概念和变量
随机推荐
[embedded C foundation] Part 1: basic data types
Getderivedstatefromprops lifecycle
Why neural networks are ineffective?
【C语言易错点】第4篇:结构体在内存中存储规则详讲
jar包
Rust from introduction to mastery 01 introduction
[FPGA] joint simulation of vivado and Modelsim
火山石投资章苏阳:硬科技,下一个10年相对确定的答案
JS method of splitting strings
Guide for using IP phone system and VoIP system
《TiDB 6.x in Action》发布,凝聚社区集体智慧的 6.x 实践汇总!
Kotlin learning notes 3 - lambda programming
Dimming and color matching cool light touch chip-dlt8ma12ts-jericho
How does the vditor renderer achieve server-side rendering (SSR)?
Vditor 渲染器如何做到服务端渲染(SSR)?
Resolve browser password echo
Tidb 6.x in action was released, a summary of 6.x practices that condense the collective wisdom of the community!
【嵌入式C基础】第1篇:基本数据类型
9、 Kubernetes configuration and storage
屈辱、抗争、逆转,三十年,中国该赢微软一次了