A really simple setup - Visual Studio 2010's default new WPF project, with MainWindow and App classes and XAML. On MainWindow there's only a simple control, a texbox, let's call it TB. All I'm trying to do is access, read and modify TB's properties from another class, such as App. That's all the code I have written, still, no can do. I prefer not to assign the values from TB's properties to variables, but manage them directly.
Asked
Active
Viewed 1,793 times
2
-
U can do this only when ur class that is managing TB properties has reference to ur MainWindow or directly to TB – Piotr Auguscik Jun 08 '11 at 19:09
1 Answers
1
The class in which you want to modify the TB, store the TB's reference in that class. Then using that reference you can modify the properties of TB.
do something like this..
public class MyClass
{
Textbox m_TextBox;
public MyClass(Textbox TB)
{
m_TextBox = TB;
}
ModifyTextbox()
{
m_TextBox.Text = "Hello World";
}
}
in the MainWindow.cs create a new instance of MyClass
MyClass myClass = new MyClass(TB);

Haris Hasan
- 29,856
- 10
- 92
- 122
-
Maybe it's me, but I don't get it. MainWindow is a class, and there's no instance of it in the project. – Gabe Jun 09 '11 at 08:55
-
When you run your WPF application its the instance of MainWindow that is running. – Haris Hasan Jun 09 '11 at 09:04
-
-
you can get the instance of MainWindow using this code `Application.Current.MainWindow` – Haris Hasan Jun 09 '11 at 09:15
-
Try to follow a basic end to end tutorial on a simple WPF Application. – Haris Hasan Jun 09 '11 at 10:07
-