当前位置:网站首页>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-136. numbers that appear only once
- Is jetpack compose completely out of view?
- nport串口服务器配置网址(串口服务器是不是网口转串口)
- kotlin学习笔记3——Lambda编程
- 《暗黑破坏神4》PS4/PS5测试版已加入PlayStation数据库
- 拥有游戏的一部分,写在我的世界禁用NFT之后
- [matlab]: FFT related problems
- [报错]使用ssh登陆到另一台机器后,发现主机名还是自己|无法访问yarn8088
- Android engineers, how to use kotlin to provide productivity?
- Led aquarium lamp touch chip-dlt8t02s-jericho
猜你喜欢

Analysis of Andriod low on memory printing principle

Understanding of vite2

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

Auto.js enables Taobao to quickly submit orders

Why is crypto game changing the game industry?

Using auto.js to realize fifaol3 brush teaching assistant

PCP parity principle arbitrage

Protective bearish strategy

Parent and child of treeselect

Definition of option basis
随机推荐
[FPGA]: ISE generates MCS file and burning process
The difference between sessionstorage, localstorage and cookies
Black cat takes you to learn EMMC protocol chapter 27: what is EMMC's dynamic capacity?
【嵌入式C基础】第8篇:C语言数组讲解
无法连接服务器怎么办(原始服务器找不到目标资源)
夜神模拟器抓包微信小程序
The essence of enterprise Digitalization
Pointnet++ Chinese Translation
我抄底了被清算的NFT,却被OpenSea上了锁
leetcode-190.颠倒二进制位
C语言学生成绩管理系统详解[通俗易懂]
FFT海浪模拟
[error] after logging in to another machine using SSH, you find that the hostname is still yourself | unable to access yarn8088
gicv3 spi register
功率放大器和匹配网络学习
《TiDB 6.x in Action》发布,凝聚社区集体智慧的 6.x 实践汇总!
kotlin学习笔记3——Lambda编程
[embedded C foundation] Part 9: basic usage of C language pointer
LeetCode每日一题(2196. Create Binary Tree From Descriptions)
国产口服新冠药阿兹夫定安全吗?专家权威解读