将数据从子窗口传递到MainWindow TextBlock [英] Pass Data from Child Window to MainWindow TextBlock
问题描述
在此示例中,MainWindow
具有一个打开Window2
的按钮.
In this example, MainWindow
has a button that opens Window2
.
Window2
具有一个写有"Hello,World!"的按钮.到MainWindow
文本块.
Window2
has a button that writes "Hello, World!" to MainWindow
textblock.
项目来源: https://www.dropbox.com/s/jegeguhycs1mewu/PassData.zip?dl = 0
将数据从Window2
传递到MainWindow
的正确方法是什么?
What is the proper way to pass data from Window2
to MainWindow
?
private MainWindow mainwindow;
public MainWindow mainwindow { get; private set; }
public Window MainWindow { get; set; }
private object mainwindow { get; private set; };
private MainWindow mainwindow = ((MainWindow)System.Windows.Application.Current.MainWindow);
this.mainwindow = mainwindow;
MainWindow
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
// Open Window 2
private void buttonWindow2_Click(object sender, RoutedEventArgs e)
{
Window2 window2 = new Window2(this);
window2.Left = Math.Max(this.Left - window2.Width, 0);
window2.Top = Math.Max(this.Top - 0, 0);
window2.ShowDialog();
}
}
窗口2
public partial class Window2 : Window
{
private MainWindow mainwindow;
public Window2(MainWindow mainwindow)
{
InitializeComponent();
this.mainwindow = mainwindow;
}
// Write Message to MainWindow
private void buttonMessage_Click(object sender, RoutedEventArgs e)
{
mainwindow.textBlockMessage.Text = "Hello, world!";
}
}
推荐答案
您正在寻找的答案非常基于实现,并且很大程度上取决于您希望Window2
作为类来做什么.
The answer you're looking for is very implementation-based and depends heavily on what you want Window2
as a class to do.
private MainWindow mainwindow;
这是可以接受的.
public MainWindow mainwindow { get; private set; }
这是可行的,但是不遵守命名约定,因为它是一个属性.通常,您可以使用它来封装字段或轻松访问计算值.
This would work but doesn't respect naming conventions because it's a property. Usually you'd use this for encapsulation of a field or for easy access to a computed value.
public Window MainWindow { get; set; }
根据您的情况,这是不可接受的,因为Window
不包含textBlockMessage
.
This is not acceptable in your context because Window
does not contain a textBlockMessage
.
private object mainwindow { get; private set; };
由于上述原因,这也不起作用.
This also wouldn't work for the same reason as above.
private MainWindow mainwindow = ((MainWindow)System.Windows.Application.Current.MainWindow);
这将起作用,甚至不允许您在Window2
实例中保留一个字段以引用MainWindow
实例.但是,每次您单击按钮时仍需要获取MainWindow
.
This would work and would even let you not keep a field for the reference to the MainWindow
instance in your Window2
instances. Still needs to get that MainWindow
everytime you click the button however.
然而,另一种有趣的操作方式是在实例化时将处理程序简单地传递给子窗口:
Another interesting way to do what you're doing however is to simply pass the handler to the child windows at instanciation:
MainWindow
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
// Open Window 2
private void buttonWindow2_Click(object sender, RoutedEventArgs e)
{
Window2 window2 = new Window2(); // No need to give a reference to the child window anymore
window2.setClickHandler((obj, ev) => {
textBlockMessage.Text = "Hello, world!"; // Direct access to the textblock.
});
window2.Left = Math.Max(this.Left - window2.Width, 0);
window2.Top = Math.Max(this.Top - 0, 0);
window2.ShowDialog();
}
}
Window2
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
}
public void setClickHandler(RoutedEventHandler handler)
{
// The handler is given to the click event.
buttonMessage.Click -= handler;
buttonMessage.Click += handler;
}
}
因此,您的Window2
类无需知道MainWindow
.
And with that your Window2
class has no need to know MainWindow
.
这篇关于将数据从子窗口传递到MainWindow TextBlock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!