Reputation: 1250
I have a wpf project setup by visual studio. I'm trying to create MVP classes, but I cannot access any xaml properties in these classes that I've created (e.g. textBox1.text or label.Content)
Files they gave me were MainWindow.xaml.cs, MainWindow.xaml and some other files. I can access those xaml properties in MainWindow.xaml.cs. But in my view.cs it show 'textBox' does not exist in the current content
. Appreciate any extra details and info
MainWIndow.xaml.cs:
namespace myApp
{
public partial class MainWindow : Window
{
private Model model;
public MainWindow()
{
InitializeComponent();
//initialize the storage
model= new Model();
}
/// <summary>
/// clear the textbox for user input
/// </summary>
private void clearInput()
{
textBox1.Text = ""; //these works fine
textBox2.Text = "";
textBox3.Text = "";
}
[...]
}
view.cs:
namespace myApp
{
public interface IView
{
void clearInput();
}
public class PView: IView
{
public void clearInput()
{
textBox1.Text = ""; //error
textBox2.Text = "";
textBox3.Text = "";
}
}
Upvotes: 0
Views: 42
Reputation: 20451
You need to give the controls (TextBox etc) in your XAML file a name, like this
<TextBlock Name="textblock1" .....
Then in your code file you can access textblock1
EDIT
However, these controls are allways private, so not visible to other classes. To make the data public you need to try something like this:
public string TextBlockText
{
get { return textblock1.Text; }
}
And then in your other classes you can use
var text = MainWindow.TextBlockText;
EDIT2
Alternatively, you can make these controls public like this
<TextBox x:Name="textBox1" x:FieldModifier="public" />
Upvotes: 1