您的位置:首页 > 编程语言

关于WPF的两个窗口之间简单的数据传递【附源代码】

2018-02-01 14:47 477 查看
目标:将窗口1(Window1)的数据传输给窗口2(Window2)。

VS版本:2015

如下图提示建立:



建立窗体1:



<Window x:Class="TestTxt.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestTxt"
mc:Ignorable="d"
Title="Window1" Height="350" Width="525">
<Grid>
<TextBox x:Name="txt_Input" HorizontalAlignment="Left" Height="23" Margin="93,137,0,0" TextWrapping="Wrap" Text="{Binding Path=Id0, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
<Button x:Name="GetNum" Content="保存" HorizontalAlignment="Left" Margin="110,220,0,0" VerticalAlignment="Top" Width="75" Click="button1_Click"/>

</Grid>
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.ComponentModel;

namespace TestTxt
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}

private void button1_Click(object sender, RoutedEventArgs e)
{
string name = txt_Input.Text.ToString();
Window2 about = new Window2();
about.getName = name;
about.ShowDialog();
}
}
}

建立窗体2:



<Window x:Class="TestTxt.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestTxt"
mc:Ignorable="d"
Title="Window2" Height="300" Width="300">
<Grid>
<TextBox x:Name="txt_Output" HorizontalAlignment="Left" Height="23" Margin="129,108,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
<Button x:Name="SetNum" Content="读取" HorizontalAlignment="Left" Margin="168,168,0,0" VerticalAlignment="Top" Width="75" Click="button2_Click"/>

</Grid>
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;

namespace TestTxt
{
public partial class Window2 : Window
{
public string getName { get; set; }//①定义一个可读可写的公用的字符串:getName
public Window2()
{
InitializeComponent();
}

private void button2_Click(object sender, RoutedEventArgs e)
{
txt_Output.Text = getName;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  WPF