WPF中实现两个窗口之间传值

在使用WPF的时候,我们经常会用到窗体之间传值,下面示例主窗口传值到子窗口,子窗口传值到主窗口的方法。

 

一、主窗口向子窗口传值

主窗口向子窗口传值主要方法就是在子窗口建立一个接收主窗口值的变量,然后实现传值。

主窗口的Xaml:

<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="向子窗体传值" FontSize="20" HorizontalAlignment="Left" Margin="342,179,0,0" VerticalAlignment="Top" Width="124" Height="34" Click="Button_Click"/>
</Grid>

视图:

主窗口的后台:

    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();
        }
    }

 

子窗口的Xaml:

    <Grid>
        <TextBlock x:Name="tb_receive" HorizontalAlignment="Left" Margin="339,75,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="29" Width="316"/>
        <Button Content="接收来至主窗口传来的值:" FontSize="15" HorizontalAlignment="Left" Margin="83,75,0,0" VerticalAlignment="Top" Width="189" Height="29" Click="Button_Click"/>
    </Grid>

视图:

子窗口后台:

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

 

传值效果图:

 

 

二、子窗口向主窗口传值

子窗体向主窗体传值主要是通过委托来执行的。

主窗体的Xaml:

<Grid>
        <Button Content="打开子窗口:" 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>

视图:

主窗体的后台:

    public partial class MainWindow : Window
    {
        public void Recevie(string value)
        {
            tb_recevie.Text = value;
        }

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            child_window child_Window = new child_window();
            child_Window.sendMessage = Recevie;
            child_Window.Show();
        }
    }

 

子窗体的Xaml:

    <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="向主窗体传值" FontSize="15" HorizontalAlignment="Left" Margin="317,181,0,0" VerticalAlignment="Top" Width="164" Height="27" Click="Button_Click"/>
    </Grid>

视图:

后台代码:

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

 

 

 

 

 

posted @ 2019-05-08 21:26  Leozi  阅读(10726)  评论(0编辑  收藏  举报