I would like to know the correct way(best looking) to set, for example, a label from a class other than my MainWindow
.
At the moment, I would do something like this:
public partial class MainWindow: Window {
public MainWindow() {
InitializeComponent();
MyClass a=new MyClass(this);
a.WriteToLabel();
}
}
class MyClass {
MainWindow parent;
public MyClass(MainWindow parent) {
this.parent=parent;
}
public void WriteToLabel() {
parent.label1.Text="Test";
}
}
But I feel like this is kind of bad practice.
It feels really bad when you have more than 20 classes and all have a field or property of parent
.
How would you solve a problem like this?