WPF:在一个窗口同时打开2个页面
我在一个窗口中同时加载两个页面时遇到问题。
这是场景。我有一个 MainWindow.xaml 和两个页面文件,分别是 LeftPage.xaml 和 RightPage.xaml。我在 MainWindow.xaml 中使用了 VS 2010 的框架控件,以便将一个窗口中的两个页面分开。
假设当我们在 LeftPage.xaml.cs 中有一段代码为 textBox1.Text = textBox2.Text 并且我们还有一个 textBox2.TextChanged 的事件处理程序时,每当用户更改输入的数据时,您在 textBox2 中键入的任何内容都会自动显示在 textBox1 上在文本框2上。
现在,我想尝试将 LeftPage.xaml 中加载的当前数据显示到 RightPage.xaml 中。假设我们在 RightPage.xaml 中有一个 textBox3
我尝试实例化 LeftPage LP = new LeftPage(); this.textBox3.Text = LP.textBox1;
它不会显示 textBox1 中的当前数据。我尝试了数据绑定、MVVM 模式,但仍然不起作用。是否可以将左侧页面当前的运行数据获取到右侧页面?谢谢
I have a problem loading two pages at the same time in one window.
Here's the scenario. I have a MainWindow.xaml and I have two page files which are LeftPage.xaml and RightPage.xaml. I used frame controls of VS 2010 into my MainWindow.xaml in order to separate the two pages in one Window.
Assume when we have a code in LeftPage.xaml.cs as textBox1.Text = textBox2.Text and we also have an eventhandler of textBox2.TextChanged, whatever you type in textBox2 will be automatically be displayed on textBox1 whenever the user changed the inputted data on textBox2.
Now, I would like to try to display the current data that is loaded in LeftPage.xaml into the RightPage.xaml. Assume we have a textBox3 in RightPage.xaml
I tried instantiating
LeftPage LP = new LeftPage();
this.textBox3.Text = LP.textBox1;
It won't display the current data that I have in textBox1. I tried databinding, MVVM pattern but still it does not work. Is it possible to get the current running data in the left page to the right page? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要保留对实际页面的引用,而不是页面的新实例。
当您执行此操作时:
LeftPage LP = new LeftPage()
您正在创建页面对象的新空实例。如果您不使用框架在页面之间导航,最好构造 UserControls 而不是页面并将它们直接托管在窗口中。
You need to keep a reference to the actual page, not a new instance of the page.
When you do this:
LeftPage LP = new LeftPage()
you are creating a new empty instance of the page object.If you are not using the frame to navigate between pages, is better the construct UserControls instead of pages and host them directly in the window.