C# 在 WPF 表单之间传递数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14433935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing data between WPF forms
提问by Vasanth91
form1
has a button btnInvoke
which invokes form2
. form2
contains a textbox
and a button btn2
.
form1
有一个button btnInvoke
调用form2
. form2
包含 atextbox
和 a button btn2
。
The user has to enter data in textbox
and press btn2
.
用户必须输入数据textbox
并按btn2
。
When btn2
is clicked form2
has to send textbox data
to form1
.
当btn2
点击form2
了发送textbox data
来form1
。
I have tried passing through constructors but I cant initiate a new instance of form1
.
我试过通过构造函数,但我无法启动form1
.
What shall I do?
我该怎么办?
回答by Habib
In your form1
define a public property.
在您form1
定义一个公共属性。
public string MyTextData { get; set; }
In your form2
on button click, get the instance of the form1
and set it property to the TextBox value.
在您form2
的按钮单击中,获取 的实例form1
并将其属性设置为 TextBox 值。
var frm1 = Application.Current.Windows["form1"] as Form1;
if(frm1 ! = null)
frm1.MyTextData = yourTextBox.Text;
In your Form1
you will get the text in your property MyTextData
在您Form1
的属性中,您将获得文本MyTextData
Its better if you following the convention for naming the windows. Use Window
instead of Form
for naming your windows in WPF. Form is usually used with WinForm applications.
如果您遵循命名窗口的约定,那就更好了。使用Window
,而不是Form
在WPF命名你的窗户。Form 通常与 WinForm 应用程序一起使用。
回答by Daniel Kelley
It may be overkill but an EventAggregator
may be a nice solution here. It would allow you to raise an event in form1
that could then be subscribed to from form2
.
这可能有点矫枉过正,但EventAggregator
在这里可能是一个不错的解决方案。它将允许您引发一个事件form1
,然后可以从form2
.
There are some details and examples of implementing an EventAggregator
in https://stackoverflow.com/questions/2343980/event-aggregator-implementation-sample-best-practices.
EventAggregator
在https://stackoverflow.com/questions/2343980/event-aggregator-implementation-sample-best-practices 中有一些实现 an 的细节和示例。
回答by Mark Hall
There are two methods that you can use. The first of which would be using ShowDialog and a public method then testing that the DialogResult is true then reading the value from the method.
您可以使用两种方法。第一个将使用 ShowDialog 和一个公共方法,然后测试 DialogResult 是否为真,然后从该方法中读取值。
i.e.
IE
if (newWindow.ShowDialog() == true)
this.Title = newWindow.myText();
The second method would be to create a CustomEvent and subscribe to it in the creating window like this.
第二种方法是创建一个 CustomEvent 并像这样在创建窗口中订阅它。
MainWindow.xaml.cs
主窗口.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Window1 newWindow = new Window1();
newWindow.RaiseCustomEvent += new EventHandler<CustomEventArgs>(newWindow_RaiseCustomEvent);
newWindow.Show();
}
void newWindow_RaiseCustomEvent(object sender, CustomEventArgs e)
{
this.Title = e.Message;
}
}
Window1.xaml.cs
Window1.xaml.cs
public partial class Window1 : Window
{
public event EventHandler<CustomEventArgs> RaiseCustomEvent;
public Window1()
{
InitializeComponent();
}
public string myText()
{
return textBox1.Text;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
RaiseCustomEvent(this, new CustomEventArgs(textBox1.Text));
}
}
public class CustomEventArgs : EventArgs
{
public CustomEventArgs(string s)
{
msg = s;
}
private string msg;
public string Message
{
get { return msg; }
}
}
回答by Christoph
Since you are working with the WPF, use CommandBindingsand Messaging. I also recommend you that you take a closser look at MVVM Frameworks, I prevere the MVVM Light Toolkit. There are a lot of HowTos for the framework, just ask google.
由于您正在使用 WPF,请使用CommandBindings和Messaging。我还建议你仔细看看 MVVM 框架,我喜欢MVVM Light Toolkit。框架的HowTos很多,去google问问就知道了。