当前位置:编程学习 > C#/ASP.NET >>

请问,WPF里在一个窗口里的textBox里输入文本,然后点Button跳转到另一个窗口,怎么能让这个窗口里的Label显示那个textBox里的文本呢?

请问,WPF里在一个窗口里的textBox里输入文本,然后点Button跳转到另一个窗口,怎么能让这个窗口里的Label显示那个textBox里的文本呢? --------------------编程问答-------------------- 全局变量,或者构造函数传递,依赖属性的绑定不知道能不能跨窗体~~ --------------------编程问答-------------------- 能再详细一点吗,我是新手 --------------------编程问答-------------------- 全局变量,或者构造函数传递
这两种都可以的
全局变量就是定义一个变量,全局的
你在输入之后赋值,在要显示那个窗口中直接显示这个变量的值

构造函数,就是写一个带参数的构造函数,参数为字符串就可以
new窗体的时候调用该构造函数就可以了 --------------------编程问答-------------------- Why not Dependency Property?

新建一个对象(继承自DependencyObject),然后创建一个Name的DependencyProperty,
如果Textbox和Label表达的同一概念的话,直接DoubleWay绑定到这个Name上就OK了

--------------------编程问答-------------------- --Window1.xaml

<Window x:Class="Wp1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <TextBox Margin="0,59,102,0" Height="26" VerticalAlignment="Top" Name="txtBox"></TextBox>
        <Button Height="23" HorizontalAlignment="Right" Margin="0,59,25,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click">Button</Button>
    </Grid>
</Window>

--Window1.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;

namespace Wp1
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Window2 wd2 = new Window2();
            wd2.label1.Content = txtBox.Text;
            wd2.Show();
        }
    }
}



--Window2.xaml

<Window x:Class="Wp1.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window2" Height="300" Width="300">
    <Grid>
        <Label Margin="70,115,88,119" Name="label1"></Label>
    </Grid>
</Window>

--OK 了. .. . .
--------------------编程问答-------------------- get set然后调用就可以了
get和set自己写
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,