如何使用C#链接两个Wpf Windows [英] How Do I Link Two Wpf Windows Using C#
问题描述
我有两个WPF窗口,即 MainWindow 和 Window1 。在 MainWindow 我有一个文本框( textbox1 )和一个按钮,在 Window1 中我有一个文本框( textbox2 ) 。
我想要完成的是当我点击按钮时, textbox1 中输入的内容现在显示在 textbox2中。
我相信我的xaml对于两个窗口都是正确的,我在我的中添加了private void button_click_1 ..... MainWindow.cs 的。但是当我尝试输入动作时:
textbox2.Text = textbox1.Text;
它无法识别 textbox2
我是WPF的新手c#所以任何帮助将不胜感激。谢谢
I have two WPF windows, that is MainWindow and Window1. In MainWindow i have a textbox(textbox1) and a button, in Window1 i have a textbox(textbox2).
What i am trying to accomplish is that when i click the button, whatever is typed in textbox1 is now displayed in textbox2.
my xaml i believe is correct for both windows and i have added the "private void button_click_1....." in my MainWindow.cs. however when ever i try to type the action:
textbox2.Text = textbox1.Text;
it does not recognize textbox2
I am fairly new to WPF and c# so any assistance would be appreciated. Thanks
推荐答案
请看我对这个问题的评论以及我过去的答案:在另一个窗口文本框中重复wpf Datagrid数据 [ ^ ]。
请学习一些OOP基础知识,否则你会在不理解基础知识的情况下学习太高级的东西。最终,它可能会带来很多挫折。
-SA
Please see my comment to the question and my past answer: Repeat wpf Datagrid data in another window text boxes[^].
Please learn some OOP basics, otherwise you are going to too advanced stuff without understanding of fundamentals. Eventually, it may end up with a lot of frustration.
—SA
首先,为两个窗口文件创建两个viewModel。
定义一个属性,它将从每个ViewModel中的文本框获取和设置值
。
为打开窗口创建NavigationService.cs类:
让NavigationService.cs
现在将以下代码放在该类文件中。
First, Create two viewModels for both window files.
Define one property which will get and set values from textbox
in each ViewModel.
Create NavigationService.cs class for opening window:
Let NavigationService.cs
Now put following code in that class file.
public void ShowWindow1Screen(Window1ViewModel window1ViewModel)
{
Window1= new Window1();
Window1.DataContext = window1ViewModel)
Window1.Owner = nicknameView;
Window1.ShowDialog();
}
然后。
创建NavigationService实例.cs类MainWindowViewModel文件。
然后
then.
Create instance of NavigationService.cs class MainWindowViewModel file.
then
Window1ViewModel window1ViewModel = new Vindow1ViewModel();
window1ViewModel.Name = MainWindowTextValue;
NavigationService navigationService = new NavigationService();
navigationService.ShowWindow1Screen(window1ViewModel);
这篇关于如何使用C#链接两个Wpf Windows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!